Hello
I have this code
As the user don't put in any numbers they get a warning and get a chance to do it again.Code:#include <iomanip> #include <ios> #include <iostream> #include <string> using namespace std ; int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (cin >> x) { ++count; sum += x; } while (count == 0 ) { cout <<"You haven't entered any values" << endl; cout << endl ; cout << "Enter all your homework grades, " "followed by end-of-file: "; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (cin >> x) { ++count; sum += x; } } // write the result streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0; }
But here the warning is displayed without giving the user a moment to do the input again.
Why is this happening and how can I solve this ?
Roelof



LinkBack URL
About LinkBacks



