I am having issues with a string. My program takes up to 5 different user inputs which are set in a loop to accomplish that. The first function of the loop asks the user for their name, along with a few other things. When the user is finished the program will allow for the next user to do the same thing. All the other inputs work fine but im using string for the user to type his name and thats giving me issues. User is supposed to type is first and last name, all in one string, then the next user can do the same. My problem is the first user can enter his name but once the 2nd user comes up it skips the name part and leaves it blank. Not sure why it keeps skipping over the entering name part. Im also writing this data to a text file which is all working fine.
This code works for the 1st user, once it goes thru the loop everything but allowing the next user to enter his name works. For the name spot it just leaves a blank spot and skips it.
This is a goofy side-effect of a questionable design decision employed by iostream.
Basically, if you mix the >> operator with getline(), you'll have this happen because the >> operator leaves whitespace in the buffer, which getline() will use instead of prompting the user for more data.
Therefore, between >> and getline(), you need to discard the whitespace that >> left in the buffer. One way to do this is with 'ws':
1 2 3 4 5 6 7 8 9 10
int a;
string b;
cin >> a;
getline(cin,b); // <- whoops! didn't prompt the user, 'b' is now an empty string
// possible fix:
cin >> a >> ws; // <- the >>ws gets rid of that pesky whitespace
getline(cin, b); // <- now this works as you'd expect
Thanks, I tried your suggestion but it put me in an infinite loop. Maybe I didnt explain it right. I have one string with the user inputed full name, i used the getline to get the whole line, then when the next user comes up in the loop, its not asking for the name anymore, it just skips it and goes to the next question. All the other questions work fine, its just the name, so im guessing it is something to do with the getline, just dont get why if its in a loop it wouldnt reprompt the user for their name again, rather it skips it.
Thanks, I tried your suggestion but it put me in an infinite loop.
Can you show me your new code? You might be doing something wrong.
Maybe I didnt explain it right.
No, you explained it well. I understand exactly what you mean. This is a common problem with iostream.
This is happening because at the end of the first loop, you are using >> to get the number of hours. This leaves the newline (whitespace) in the buffer, so when you try to get the name for the 2nd employee, it sees the newline and thinks that the user input an empty string.
You have to purge the whitespace.
If >> ws isn't working, the other thing you can do is use ignore to discard characters up to the next newline:
1 2 3 4 5
cin >> Hours;
cin.ignore(100); // ignore/erase the next 100 characters or until the next newline
getline(cin, Name); // then get the name. Now there's no whitespace to confuse getline
I do have one other question that I cant figure out either. For that same code where the user is entering their first and last name, is there away to keep it as one string and have it display it in that text file but as last name then first name, so the opposite order of which it was typed?