I stuck in reading the text and integer from the file using ifstream with c++.
My goal is to read the firstname and lastname following by 10 quiz score. It is working but if the any quiz score is missing, the program will not read the next line.
ifstream inputStream;
inputStream.open("input2.dat");
if (inputStream.fail() )
{
cout << "Error opening the file.\n";
exit(1);
}
ofstream fout;
fout.open("output.dat");
string firstname, lastname;
int quizScore = 0;
double sum = 0;
while (inputStream >> firstname >> lastname)
{
sum = 0;
fout << firstname << ' ' << lastname;
for (int i = 0; i < 10; i++)
{
inputStream >> quizScore;
sum += quizScore;
fout << " " << quizScore;
}
cout << firstname << "\t" << sum / 10 << "\n";
fout << " " << sum / 10 << "\n";
}
inputStream.close();
fout.close();
input2.dat
Tony Dinozzo 50 45 40 35 30 15 10 5
Ziva David 50 45 50 45 38
Timothy Mcgee 15 45 25 45 28 50 35