help with loop

Write a program using sentinel loop that gets the unspecified number of student names and and then 3 exam scores of each students and calculates the average.

my code:
#include <iostream>
#include <string>

using namespace std;

int main()

string studentNames;

double total;
string sentinel="No";

cout<< "Enter the names or enter No to stop"<< endl;

getline (cin, studentNames);
total = 0;
for (int score=1; score<=3; score++)
double testScore ;
cout << "Enter score " << score << " for ";
cout << "student " << studentNames << ": ";
cin >> testScore;
total += testScore;

}while ( studentNames!=sentinel);

return 0;


my problem is how to make the user enter the student name again after entering three scores? i need help on that only.
You're very close. You actually are getting the student name each time, but the program is skipping over it because of a cin buffering issue. This happens when you mix getline with 'cin >>' commands.

To fix, throw a cin.sync(); before the getline call.
thnks for the reply , disch. But cin.sync didnt work.
Try this instead of sync:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

This effectively discards all characters remaining in input stream buffer cin until (and including) a newline '\n' character appears. If there is no newline character the whole buffer is cleared.

See also:
Topic archived. No new replies allowed.