# c++ inputing a string with spaces

## Recommended Posts

spree    100
When I Input "hello how are you ?" my output is: hello how are you Why the cin ignores the spaces ? How can I input a string which includes spaces?
while(1)
{print("\ncmd:");
cin>>msg;
if(strcmp(msg,"exit")==0) break;

cout<<msg<<endl;
}



##### Share on other sites
dave    2187
Hi,

strange, i just wrote this...

#include <iostream>#include <string>using std::string;using std::cout;using std::cin;int main(int arc, char* argv[]){	string message;	cin >> message;	cout << message;	while (1);	return 0;}

and typed "hello how are you"

and the output is just...

hello

After stepping through i found that cout is not the problem, but cin is. It only puts the first word into 'message'.

Anyone care to explain why this is?

ace

##### Share on other sites
happybara    312
just use cin.getline()

##### Share on other sites
JohnBSmall    881
cin.getline is great, but doesn't fit with using std::string very well.

Try:
std::getline(std::cin, your_string);

John B

##### Share on other sites
Zahlman    1682
Input string data is "tokenized" by default, i.e. broken at whitespace boundaries, with whitespace getting skipped over. This inherits from the behaviour of the old stdio from C. My best guess is that the IO library was designed to make it easier to write... C compilers. :)

Anyway, what you want is the free function 'getline' in iostream, in the std namespace:

getline(ifstream&, string, {char})

Reads a line from the input stream and sets the string contents to the result. If the character argument is provided, it is treated as the delimiter for the line (default is \n).

The member function version of getline reads into a character array instead. I have no idea why they set things up this way (I smell "backward compatibility" though), it's quite a mess :( Please please use the version that reads into a std::string; i.e. use std::strings. It makes for a much happier Zahlman :)

string msg;while(1)	{cout << "\ncmd:" << flush;	 getline(cin, msg);	 if (msg == "exit") break; // see how much nicer it is	 cout<<msg<<endl;	}

##### Share on other sites
JohnBSmall    881
Quote:
 Original post by ace_lovegroveAnyone care to explain why this is?

Because that's what the >> operator for input streams does. That's just the way it is. It's not a bug, it's not odd - just check the docs, and they'll tell you that's what it does.

John B

##### Share on other sites
spree    100
Thanks alot guys :), one more question:
is std::string is backword compatible for functions which want a char * as an input argument ?

##### Share on other sites
MaulingMonkey    1730
Quote:
 Original post by spreeThanks alot guys :), one more question: is std::string is backword compatible for functions which want a char * as an input argument ?

Yes. A horrible example which uses printf:

std::string pie = "I like pie.";printf("%s" , pie.c_str());

Yes, std::string::c_str() returns "const char *". Note that if your string changes, the memory pointed to might very well be moved... so don't do:

const char * str1 = pie.c_str();pie += " Fnord.";printf("%s" , str1);

And expect your program to work right. Because it won't.