//************************************************************* // File: highestAvg.cpp /* Description: - Given a list of midn names/alpha codes followed by a comma-separated list of grades followed by a ; - Print out the name of the student with the highest average (and the average). //********************************************************************* // v1 -- simplified input (one word name, one grade) // Jones m040101 85 // Smith m040201 99 // Xenon m050400 78 // Change to work for v2 input (one word name, multiple grades per student): // Jones m090101 85, 95, 22, 54; // Smith m090201 99, 98; // Xenon m090400 78, 80, 82, 84, 86, 88, 90; ***********************************************************/ #include #include #include using namespace std; int main() { double average, maxAverage=-1; // why is -1 safe here? string name, alpha, maxAverageName; char junkCh; double gradeTotal, grade; int gradeCounter; // Open the file ifstream fin("grades1-input.txt"); // Process each line, checking if new max is found // Keep going so long as we don't run out of names while (fin >> name) { // Already have name, now get rest of line fin >> alpha; //initialize gradeTotal gradeTotal = 0; gradeCounter = 0; junkCh = ','; //average grades for specific name while (junkCh != ';') { fin >> length >> note; gradeTotal = gradeTotal + grade; gradeCounter++; fin >> junkCh; } //compute average average = gradeTotal / gradeCounter; // If new max is found, update if (average > maxAverage) { maxAverage = average; maxAverageName = name; } } cout << maxAverageName << " had the highest average, a " << maxAverage << endl; return 0; }