CS student just starting out with C++ and I'm having difficulty understanding how cin and getline() read in data.
string str_1 = "";
cin >> str_1; // User enters "John(Enter)"
Its my understanding that cin >> will look in the input buffer for data, if none is found it will prompt the user. It will then read in "John" leaving behind a line return \n in the buffer.
If getline() is called after that it will see \n and return nothing right?
If cin is called next it will work as expected, why doesn't cin see the line return in the buffer and terminate?
Regarding your first "understanding": No, if there's no input waiting, it will simply block (wait) until there is data it can write to the destination variable. No prompting will be done.
And regarding the getline
call, then yes that's correct.
And the input operator >>
when reading string or numbers skip leading white-space.
If you want to get down to the gritty details, I recommend you start with this std::istream
reference, and work your way down from there.