3.7 Formatted input and output 79
/*------loop over student grades----------------*/
while(file2 >> grade[i][j]) // read the grades
{
cout << setw(3) << right << grade[i][j] << " ";
j++;
}
cout << endl;
file2.clear(); // allow for more reads
i++;
}// end of loop over students
file2.close();
return 0;
}
An interesting feature of the code is the presence of two nestedwhile
loops of reads. When a false name-read is encountered, the outer loop is exited.
When a false grade-read is encountered, the inner loop is exited, and thecin
flag is cleared by issuing the command:
file2.clear();
to allow for additional name readings.
If the filegrades.datcontains the data:
Johnson Bob 8 10
Kerr Jean 9 10 9
Wang J-Y 8 10 9
then the output on the screen is:
1 Johnson Bob 8 10
2 Kerr Jean 9 10 9
3 Wang J-Y 8 10 9
Problems
3.7.1.Print the value ofRANDMAXset by your compiler.