weird problem

{
int grade, total=0;
double avg;
char ans = ' ';
string name;

do{
cout << "\n\nEnter the student's name: ";
getline(cin, name);

for (int x = 1; x < 6; x++)
{
cout << "\nEnter grade: ";
cin >> grade;

total = total + grade;
}

avg = total/5;

cout << "\n\n\nName of student : " << name;
cout << "\n\nAverage : " << avg;

cout << "\n\n\n\nWould you like to enter another student's grade? (Y/N): ";
cin >> ans;


}while(ans == 'Y' || ans == 'y');

system ("pause");
return 0;
}

Everytime i run this program when the do while loop loops it starts from 'enter grade' instead of 'enter the students name'?
I think this is probably going to be something to do with the input stream not being flushed/cleared.

I would advise using either getline or the >>, not a mixture of both. If you have a search, there are a few posts covering this.

On another note, avg = total/5; isn't going to do what you want. Dividing an integer by another integer isn't going to yield a double.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
int grade, total=0;
double avg;
char ans = ' ';
string name;

do{
cout << "\n\nEnter the student's name: ";
getline(cin, name);

for (int x = 1; x < 6; x++)
{
cout << "\nEnter grade: ";
cin >> grade;

total = total + grade;
}

avg = total/5;

cout << "\n\n\nName of student : " << name;
cout << "\n\nAverage : " << avg;

cout << "\n\n\n\nWould you like to enter another student's grade? (Y/N): ";
cin >> ans;
cin.ignore();//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< :)

}while(ans == 'Y' || ans == 'y');

system ("pause");
return 0;
}
Thanks to both of you! <3
Topic archived. No new replies allowed.