// Program file: infile4.cpp // this program demonstrates reading data from a file // using global variables #include #include #include using namespace std; // function prototypes void heading(); void getrecord(); float calcaverage(); void writerecord(); // global variable declarations long int ssn; int grade1, grade2; float average; ifstream infile; int main() { infile.open("grade2.dat"); heading(); cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(1); getrecord(); while (! infile.eof()) { average = calcaverage(); writerecord(); getrecord(); } getch(); return 0; } void heading() { cout << "SocSecNum Grade1 Grade2 Average" << endl; } void getrecord() { infile >> ssn >> grade1 >> grade2; } float calcaverage() { float a = 0; a = (grade1 + grade2) / 2.0; return a; } void writerecord() { cout << setw(9) << ssn << setw(5) << " " << setw(3) << grade1 << setw(5) << " " << setw(3) << grade2 << setw(5) << " " << setw(5) << average << endl; }