Reputation: 487
I am using getline() in a loop. During the first loop everything runs okay except for the last getline(). During the second loop the first getline() seems to have been skipped. Here is the loop:
while(true)
{
cout <<endl<< "Enter Student's Name: ";
getline(cin,tmp_name);
cout << "Enter Student's RegNo: ";
getline(cin,tmp_regno);
cout << "Enter Student's marks: ";
cin>>tmp_marks;
mystudents.push_back(student(tmp_name,tmp_regno,tmp_marks));
mystudents[no_ofStudents].getGrade();
no_ofStudents++;
cout<<endl<<endl<<"Do you wish to continue? To continue enter yes or any other key to stop: ";
getline(cin,continue_stop);
if (continue_stop!="yes"&&continue_stop!="YES")
break;
}
Upvotes: 0
Views: 3269
Reputation:
cin.ignore(numeric_limits<streamsize>::max(),'\n');
Try using this after cin
Upvotes: 0
Reputation: 1362
And another thing
if (continue_stop!="yes"&&continue_stop!="YES")
break;
This will break while
loop at wrong time.
Upvotes: 1
Reputation: 29724
cin >> tmp_marks;
leaves '\n'
in input stream and you are reading it in the next read
std::getline( std::cin,continue_stop);
You can ignore this character with:
std::cin>>tmp_name;
std::cin.ignore();
std::cout<<std::endl<<std::endl<<"Do you wish to continue?";
std::getline( std::cin,continue_stop);
if (continue_stop!="yes"&&continue_stop!="YES")
break;
Upvotes: 1
Reputation: 206577
cin >> tmp_marks;
leaves the newline character ('\n') in the input stream. You have to figure out a way to read everything after that until the next newline.
Upvotes: 1