I will use string first_name, second_name as it is less complicated. But i would like to see how its done with the getline
1 2 3 4 5 6 7 8 9 10
{
for (size_t count = 0; count<NR_OF_RESPONDERS; ++count)
{
cout<<"Enter your name: ";
cin>>name[count]
cout<<" Do you call him Jesus Christ or Messiah?: ";
getline(cin,youName[count]);
}
cout<<"---------------";
}
it actually skips the part where you have to insert your choice and goes back to Enter name
You are experiencing the tiny nightmare which results from mixing formatted (cin >>) input operations with unformatted ones like getline.
cin >> leaves the \n in the stream then getline sees that leftover \n as end of line.
The best way to fix this IMO is to avoid mixing the 2 input methods.
Use getline for both:
1 2 3 4
cout<<"Enter your name: ";
getline(cin,name[count]);
cout<<" Do you call him Jesus Christ or Messiah?: ";
getline(cin,youName[count]);
This also permits the user to enter 1st + last name.
@tipaye I hadn't noticed your post, not sure why.
User may enter one or two words, it is not known, so getline is really needed here.
The 1st method would hang expecting input for last_name whether the user intends to enter a 2nd name or not.
@fun2code Thanks for pointing that out, it usually catches people out.
@OP I probably should have elaborated, I'd expected you'd be giving your users better instructions than shown in the post. If you really want to use cin >> twice, with first_name, last_name, then I suggest explicitly using multiple statements, like:
1 2 3 4 5 6 7 8 9 10 11 12
string first_name;
cout << "Please enter first name ";
cin >> first_name;
// check stream state ??
string second_name;
cout << "Please enter second name ";
cin >> second_name;
// check stream state ??
Don't forget to check for input errors, however unlikely.