cin stream operator behaviour

I’ve been tinkering with with cin and getline to make sure I fully understand what’s going on. In the code below, I replaced the two bolded lines with either cin or getline and printed the output at the bottom to see what happens.

 

string name1, name2;

cout << "enter name1";
cin >> name1; <em><-- replace with either cin or getline</em>
cout << "enter name2";
getline(cin, name2); <em><-- replace with either cin or getline</em>
cout <<"name1"<< name1 <<"name2"<< name2 << endl;

 

Using John as the input for name1, I get the following results.

1. When I use getline, followed by getline, the program runs as expected. I’m asked to enter name1 (John) and name2 (anything), which is great.

2. When I use getline, followed by cin, the program still runs as expcted, Im asked to enter name1 (John) and name2 (anything), which is good.

3. When I use cin, followed by getline however, I enter John for name1 as before, but I’m not prompted for the getline because the newline character ‘\n’from the [Enter] key gets stored in the buffer and gets read by getline.

4. The only scenario that doesn’t quite make sense is if I use cin, followed by cin. I’m still prompted for the name2, even though I thought that a newline character from the first cin would be read into this. I’m concluding from this operation that only getline (a C++ function) and cin.get (a C-string function) are able to “see” that ‘\n’ whereas cin doesn’t. I really hope that observation is correct.