How to make an output file / text alignment

Can someone tell me how to output data to a output text file? I know I am missing the outfile << whatever << statement but I cannot figure out how to get outfile to print everything in the function print to the output text file.

I am also having problems aligning the test scores and grades in a column. can someone tell me how to correct this issue?

thanks!

Last edited on
First of all you should open the file only when you need it and also check that it is open. There is no need to declare the file variable global.

A simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void SaveStudentData()
{
   ofstream dest("output.txt");
   if (!dest)
   {
      cerr << "\aError opening output file.";
      return;  // Maybe terminate program?
   }
   // Check that there are 20 students in the array
   for (int i=0; i < 20; i++)
  {
     outFile << Student[i].studentLName << '\t' << Student[i].studentFName << '\t' <<
     Student[i].testScore << '\t' << Student[i].grade << '\n';
  }
}
@thomas1965 thanks for the help. I had it like that, but my professor wanted us to make the file variables global.
but my professor wanted us to make the file variables global.


That's not a problem. Just use the global file variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SaveStudentData()
{
   if (!outFile)
   {
      cerr << "\aError - output file not open.";
      return;  // Maybe terminate program?
   }
   // Check that there are 20 students in the array
   for (int i=0; i < 20; i++)
  {
     outFile << Student[i].studentLName << '\t' << Student[i].studentFName << '\t' <<
     Student[i].testScore << '\t' << Student[i].grade << '\n';
  }
}
Topic archived. No new replies allowed.