cin vs getline(cin, information) ?

Hello! I'm brand new to C++, and I have a question regarding cin.

So I see different ways to retrieve user information, and I've always used
cin >> information;

But I would always come across things such as
getline(cin, information);

As a new student to C++ this is irking me, and throwing me off as well. I was hoping someone could please explain the different in between the two to me. Any information given would be extremely helpful.

Thank you!
getline only works on strings cin >> information works all data types(except user defined data types unless >> is overloaded).

with getline you can specify the delimiter by default '\n' is used.
cin >> information uses any whitespace as a delimiter this includes spaces, newlines, tabs etc.

getline removes the delimiter from the input stream cin >> information does not.

because getline allows you to specify the delimiter you can input data with spaces. So with getline you can input a persons name say john smith into a string variable. If you try to do this with cin >> information your variable will contain john because the space between john and smith is a delimiter.
Last edited on
Thanks for the help, clears up a lot of issues I've had reading certain codes!
Topic archived. No new replies allowed.