I need to add a statement to the body of the
while loop so that the score will be saved in the array GPA. I've tried finding a forum already because this seems like it should be incredibly easy but I haven't figured out how yet. Here is my code:
#include <iostream> #include <fstream>
using namespace std;
int main()
{
ifstream infile;
string name;
double score;
int size = 0; // size of data file (keeps track of the num of
records)
infile.open("datafile"); // open the file 'datafile' if (infile.fail()) // if unable to open, show an error msg
{
cout << "ERROR: can't open file!" << endl;
exit(1);
}
infile >> name >> score; // read the first student's data while(! infile.eof()) // as long as we have not reached the
end of file
{
size++; // update the num of records
cout << name << score; // output student record
infile >> name >> score; // get another student's data
}
cout << "Number of students = " << size;
infile.close();
}
To help you out, I need a little more information:
1. Are there more than one record per student?
2. Is the file sorted?
Also, I find it easer for totals to do all of your reads within the while-loop. In your code, you will have to store the first student's name and score outside of the loop, and you have to add extra code to calculate your GPA. I suggest the following change:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
...
while( 1 )
{
infile >> name >> score;
if( infile.eof() )
break;
/* Other processing here */
} /* while( 1 ) */