Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a C++ question write a program that saves records to a sequential access

ID: 665776 • Letter: T

Question

This is a C++ question

write a program that saves records to a sequential access file named Trythis17.txt. Each record should appear on a separate line and contain two fields separated by the umber sign (#). The first field should contain numbers from 10 through 25. The second field should contain the square of the number in the first field. For example, the the first record will contain 10#100 followed by a newline character. Display the message "Numbers saved in file" if the program was able to save the numbers.

Explanation / Answer

#include <iostream.h>
// #include <iostream>
#include <stdlib.h>
#include <fstream.h>
// #include <fstream>
// using namespace std;
int main() // start c++ program Trythis17.cpp
{

   // open the file first

ofstream filePtr;    // declare a file pointer variable
filePtr.open ("e:/Trythis17.txt");   // opening the file for writing/appending
// filePtr << "Writing this test line first time to the file just opened. ";
// filePtr << "Writing this test line second time to the file just opened. ";

   for(int i = 10; i<= 25; i++)    // run the for loop from 10 to 25 inclusive
   {
       filePtr << i << " # " << i * i;       // i*i will gice square(i)
       filePtr << " ";     // add a new line to the output text file
   }

   cout << " Numbers saved in file successfully";

filePtr.close();   // we are all set, hence close the file and release it
return 0;   // return zero as integer from main to indicate good finish


} // end main

output ( or the contents of the text file Trythis17.txt fro the windows folder after a run):

10 # 100
11 # 121
12 # 144
13 # 169
14 # 196
15 # 225
16 # 256
17 # 289
18 # 324
19 # 361
20 # 400
21 # 441
22 # 484
23 # 529
24 # 576
25 # 625

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote