#include <iostream> #include <iomanip> using namespace std; int main() { int counter = 0; double score, averagescore, totalscore = 0.0; cout << "Enter the score of a student, or -1 if no more students are left.\n"; cin >> score; while (score < -1 || score > 100) { cout << "\nThat is not a valid answer please enter a percent between -1 and 100\n"; cin >> score; } while (score != -1) { totalscore += score; cout << "\nEnter the score of a student, or -1 if no more students are left.\n"; cin >> score; while (score < -1 || score > 100) { cout << "\nThat is not a valid answer please enter a percent between -1 and 100\n"; cin >> score; } counter++; } averagescore = totalscore / counter; cout << "\nThe average score of the class is: " << averagescore << '%' << endl; }
cin.fail
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)