problem with c++ while loop

this is my code i did with c++. and the program should only accept numbers. it need four correct input to proceed to next process. and should ignore wrong inputs. the problem is, it accepts 8 correct inputs. it ignores the first 4 correct answer and get the values of the last 4 correct input. and then get the average of the four values .it should only ask and get four values. what is wrong with my loop?

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(int argc, char** argv) {

float a=0, b=0, c=0, d=0, grade = 0;


cout << " Enter your Grade on Quiz no. 1 : \t";

cout << " Enter your Grade on Quiz no. 2 : \t";

cout << " Enter your Grade on Quiz no. 3 : \t";

cout << " Enter your Grade on Quiz no. 4 : \t";

while(!(cin>>a))
{
cout<<"Wrong. Try again."<<endl;
cin.clear();
cin.ignore();
system ("cls");
cout << " Enter your Grade on Quiz no. 1 : \t";
cin >> a ;

if ( cin>> a){
break;
}
}

while(!(cin>>b))
{
cout<<"Wrong. Try again."<<endl;
cin.clear();
cin.ignore();
system ("cls");
cout << " Enter your Grade on Quiz no. 2 : \t";
cin >> a ;

if ( cin>> b){
break;
}
}

while(!(cin>>c))
{
cout<<"Wrong. Try again."<<endl;
cin.clear();
cin.ignore();
system ("cls");
cout << " Enter your Grade on Quiz no. 3 : \t";
cin >> c ;

if ( cin>> c){
break;
}
}

while(!(cin>>d))
{
cout<<"Wrong. Try again."<<endl;
cin.clear();
cin.ignore();
system ("cls");
cout << " Enter your Grade on Quiz no. 4 : \t";
cin >> a ;

if ( cin>> d){
break;
}
}

system ("cls");

cout << " Quiz#" << "\t\tGrade\n";
cout <<" 1\t\t" << a << endl;
cout <<" 2\t\t" << b << endl;
cout <<" 3\t\t" << c << endl;
cout <<" 4\t\t" << d << endl;
cout << "\n";

cout << " TOTAL AVE."<< "\t\tREMARKS\n";

grade = (a+b+c+d)/ 4;

if (grade> 94 && grade< 101 ) {

cout << " " << setw(4)<< left<<grade << "\t\tExcellent!\n\n";
}

else if (grade> 89 && grade< 95){

cout << " " << setw(4)<< left<< grade << "\t\tVery Satisfactory!\n\n";
}

else if (grade> 84 && grade< 90 ) {

cout << " " << setw(4)<< left<<grade << "\t\tSatisfactory!\n\n";
}

else if (grade> 79 && grade< 85 ) {

cout << " " << setw(4)<< left<<grade << "\t\tFine!\n\n";
}

else if (grade> 74 && grade< 80 ) {

cout << " " << setw(4)<< left<<grade << "\t\t\Fair!\n\n";
}

else if (grade> -1 && grade < 75 ) {

cout << " " << setw(4)<< left<<grade << "\t\t\Poor.\n\n";
}

else {
cout << " " << setw(4)<< left<< " " << "\t\t\Invalid Input.\n\n";
}

system ("PAUSE");
return EXIT_SUCCESS;
}


Last edited on
In each while loop you have the input twice, e.g.:
1
2
3
4
5
6
cout << " Enter your Grade on Quiz no. 2 : \t";
cin >> a ; // First input a (remove this line)

if ( cin>> b){ // Second input b
break;
}


So remove for all while loops the first input.

Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.