In this case, presumably the password doesn't contain spaces?
If that's the case, then use
instead of
|
getline(cin, my_pass, '\n');
|
Well - your code has both, and that's the cause of the problem. Stick to one or the other and it should work.
What happens is, after the
cin >>
at line 12, there is a trailing newline left in the input buffer. Then, when the
getline()
at line 13 is reached, the function will read into the string until a newline is found, resulting in an empty string.
On some occasions (not here) you do need to have both cin >> something and getline(something else). If you did need to do that, then you'd need to empty the input buffer after the cin >> statement.
One way to do that would be
the
ws
means read and discard any whitespace, which will include the unwanted newline.
Another way is to use ignore, for example
1 2
|
cin >> my_pass;
cin.ignore(1000, '\n'); // ignore up to 1000 characters, or until a newline is found
|