I am a beginner programer trying to learn C++. I am trying to read the following information from a file:
Dean DeFino 88 98 99
Sally Johnson 78 89 82
Bill Benny 75 79 81
Thomas Billows 78 84 89
However, I get the following output and I can't figure out why:
Dean DeFino 88 98 99
0 0 0
Sally Johnson 78 89 82
0 0 0
Here is the code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
const int NAMESIZE = 15;
const int MAXRECORDS = 50;
struct Grades // declares a structure
{
char name[NAMESIZE + 1];
int test1;
int test2;
int final;
};
typedef Grades gradeType[MAXRECORDS];
// This makes gradeType a data type
// that holds MAXRECORDS
// Grades structures.
void readIt(ifstream&, gradeType, const int);
int main()
{
ifstream indata;
indata.open("graderoll.dat");
int numRecord = 4;
gradeType studentRecord;
if(!indata)
{
cout << "Error opening file. \n";
cout << "It may not exist where indicated" << endl;
return 1;
}
readIt(indata, studentRecord, MAXRECORDS);
// output the information
for (int count = 0; count < numRecord; count++)
{
cout << studentRecord[count].name << setw(10)
<< studentRecord[count].test1
<< setw(10) << studentRecord[count].test2;
cout << setw(10) << studentRecord[count].final << endl;
}
return 0;
}
void readIt(ifstream& inData, gradeType gradeRec, const int max)
{
int total = 0;
inData.get(gradeRec[total].name, NAMESIZE);
while (inData)
{
inData >> gradeRec[total].test1;
inData >> gradeRec[total].test2;
inData >> gradeRec[total].final;
total++;
inData.clear();
inData.get(gradeRec[total].name, NAMESIZE);
}
}
Any suggestions to help me out with this?