I am trying to understand the way the getline function works. In the following example, I enter "Hello There!" and use the cin object to take in input from the user. I do this to demonstrate that the cin will stop reading in input after a space is entered by the user.
Below this I am using the getline function to demonstrate the correct way to do this, but don't understand why it outputs the way that it does on the terminal window. Please explain. Thank you.
#include <iostream>
using namespace std;
int main()
{
string greeting;
// cout << "Enter \"Hello There!\": ";
cin >> greeting; // Once the cin object sees a space, it stops. This is why the cin object only grabs the first word.
cout << "greeting: " << greeting << endl;
cout << "Enter \"Hello There!\": ";
getline(cin, greeting);
cout << "greeting: " << greeting << endl;
return 0;
}
Source Code: https://replit.com/@gurbanishvili/getline-for-Strings#main.cpp