• Create Account

Posted 07 February 2013 - 04:33 PM

I just wanted to expand on the "Use getline instead of "cin>>" answers.  While they're correct I feel it'd be appropriate to expand more on them so you know what is actually happening and why it is.

cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction. This behavior may or may not be what we want; for example if we want to get a sentence from the user, this extraction operation would not be useful.

In order to get entire lines, we can use the function getline, which is the more recommendable way to get user input with cin:

Example:

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;

output:
Hello Juan Soulie.
What is your favorite team? The Isotopes
I like The Isotopes too!


using cin>> will leave what was left after the space you typed in the buffer.  So next time it is called it sees something in the buffer and reads it.

EDIT: This was taken from here: http://www.cplusplus.com/doc/tutorial/basic_io/

This should give you a good way of how C++ Console I/O works.

Posted 07 February 2013 - 04:31 PM

I just wanted to expand on the "Use getline instead of "cin>>" answers.  While they're correct I feel it'd be appropriate to expand more on them so you know what is actually happening and why it is.

cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction. This behavior may or may not be what we want; for example if we want to get a sentence from the user, this extraction operation would not be useful.

In order to get entire lines, we can use the function getline, which is the more recommendable way to get user input with cin:

Example:

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;

output:
Hello Juan Soulie.
What is your favorite team? The Isotopes
I like The Isotopes too!


using cin>> will leave what was left after the space you typed in the buffer.  So next time it is called it sees something in the buffer and reads it.

Posted 07 February 2013 - 04:25 PM

I just wanted to expand on the "Use getline instead of "cin>>" answers.  While they're correct I feel it'd be appropriate to expand more on them so you know what is actually happening and why it is.

getline will read until it reaches the \n character.  That means everything will be read it before the new line.  With std::cin>> everything after the space is being left in the buffer so it is added next time.

cin>> will basically read until a space or \n is found and everything after that is left in the buffer.

PARTNERS