/*************************************************/ // gradeAvg5.cpp // R. A. Hillyard // Last Modified: 02/16/2001 // // Determine the class average. // wrapper loop to allow multiple runs of code /*************************************************/ #include <iostream> using namespace std; int main() { int gradeSum; //sum of all grades int counter; //number of grades entered int grade; //one grade int average; //class average char choice; //holds the user choice to continue do { //common mistake to not initialize variables before each loop counter = 0; //how many times through loop gradeSum = 0; //set sum to zero before entering loop cout << "Enter grade, -1 to end: "; //get first value outside the loop while((cin >> grade)) { if(grade == -1) break; gradeSum += grade; counter++; cout << "Enter grade, -1 to end: "; }//end while //compute average and print results average = gradeSum/counter; cout << "\nClass average is: " << average << endl; //see if user wants to continue cout << "\nWould you like to calculate another average? [Y/N]: "; cin >> choice; }while(choice=='y' || choice=='Y'); return 0; } /********************Program Output*************************** Enter grade, -1 to end: 68 Enter grade, -1 to end: 93 Enter grade, -1 to end: 88 Enter grade, -1 to end: 74 Enter grade, -1 to end: 65 Enter grade, -1 to end: -1 Class average is: 77 Would you like to calculate another average? [Y/N]: y Enter grade, -1 to end: 99 Enter grade, -1 to end: 94 Enter grade, -1 to end: 93 Enter grade, -1 to end: 78 Enter grade, -1 to end: 92 Enter grade, -1 to end: -1 Class average is: 91 Would you like to calculate another average? [Y/N]: n */