I have been having some issues with a program I am writing and I am using a while loop to compute averages like:
while(cin >> x){
++count;
sum +=x;
}
avg = sum / count;
but I have three of these in my program to compute a final grade (averaging quizzes, labs, and assignments) and after I input the assignment grades or whichever average comes first my program ends after outputting everything I say to output but it doesn't let me input anything else. How can I fix it so I can input all the values I need?
This is my program:
//Program to calculate final grade in whatever subject
#include<iostream>
using namespace std;
int main(){
//How much everything in the course is worth, set to zero if course doesn't have that section.
cout << "How much are assignments worth in percent? ";
double assign, labs, quizzes, midterm1, midterm2, midterm3, final;
string end;
cin >> assign;
cout << "How much are labs worth in percent? ";
cin >> labs;
cout << "How much are quizzes worth in percent? ";
cin >> quizzes;
cout << "How much is midterm 1 worth in percent? ";
cin >> midterm1;
cout << "How much is midterm 2 worth in percent? ";
cin >> midterm2;
cout << "How much is midterm 3 worth in percent? ";
cin >> midterm3;
cout << "How much is the final worth in percent? ";
cin >> final;
cout << "The grade you need on the final to get an A+ is: " << final_Apls << endl;
cout << "The grade you need on the final to get an A is: " << final_A << endl;
cout << "The grade you need on the final to get an A- is: " << final_Amin << endl;
cout << "The grade you need on the final to get a B+ is: " << final_Bpls << endl;
cout << "The grade you need on the final to get a B is: " << final_B << endl;
cout << "The grade you need on the final to get a B- is: " << final_Bmin << endl;
cout << "The grade you need on the final to get a C+ is: " << final_Cpls << endl;
cout << "The grade you need on the final to get a C is: " << final_C << endl;
cout << "The grade you need on the final to get a C- is: " << final_Cmin << endl;
cout << "The grade you need on the final to get a D is: " << final_D << endl;
cout << "The grade you need on the final to get an F is: " << final_F << endl;
Try putting cin.sync() after each of your cin.clear() statements.
EDIT: Ehh... sync() is implementation specific. This is probably a better alternative: std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
You'll need to #include <limits>
Basically giving 'end' as input is putting cin into an error state, which makes it leave the loop, which is why you are calling cin.clear(). However, you're ALSO leaving characters in the input buffer, which on subsequent cin << whatever; is putting cin right back into an error state.
A) use code tags. They looks like <> under format section
B) YOu should clear cin state and remove everything in buffer after fail (after exiting from your while loop)