/***************************************************/ // L Frye // CSC135010 - Spring 2008 // Program #1 // Due: Feb 14, 2008 // Filename: avgs.cpp // Description: This program will find a student's test average /***************************************************/ #include #include #include #define NUMTESTS 3 using namespace std; int getInput(ifstream &infile, string &name, float &test1, float &test2, float &test3); float calcAvg(float test1, float test2, float test3); void print(string name, float avg); int main() { // variable declarations string name; // student's name float test1, test2, test3; float avg; // test average ifstream infile; int res; // Open file infile.open("averages.data"); if (infile.fail()) { cout << endl; cout << "Error opening input file averages.data" << endl << endl; return 0; } // end if fail cout << fixed << showpoint; // print in fixed-point with decimal cout << setprecision(2); // print two places after decimal // read first line of file res = getInput(infile, name, test1, test2, test3); while (res == 1) res = getInput(infile, name, test1, test2, test3); while (!infile.eof()) { // processing avg = (test1 + test2 + test3) / NUMTESTS; // print results print(name, avg); // get input res = getInput(infile, name, test1, test2, test3); while (res == 1) res = getInput(infile, name, test1, test2, test3); } // end eof loop infile.close(); return 0; } int getInput(ifstream &infile, string &name, float &test1, float &test2, float &test3) { getline(infile, name); infile >> test1; if (test1 < 0) { infile.ignore(100, '\n'); return 1; } // end if infile >> test2; if (test2 < 0) { infile.ignore(100, '\n'); return 1; } // end if infile >> test3; if (test3 < 0) { infile.ignore(100, '\n'); return 1; } // end if infile.ignore(10, '\n'); // skip over newline character return 0; } // end function getInput float calcAvg(float test1, float test2, float test3) { float avg; avg = (test1 + test2 + test3) / NUMTESTS; return avg; } // end function calcAvg void print(string name, float avg) { cout << left << setw(25) << name; cout << "Average = "; cout << right << setw(7) << avg << "%" << endl; return; } // end function print