Sentences in Strings

So I'm fairly new to C++ and I'm having a bit of trouble getting a full sentence in a string. The program works just fine when I enter just one word without any spaces, but when I enter two words with a space between them, the window just closes instead of continuing on with the program. Any help as to why this is happening and how I can fix it would be awesome.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string a;
cin>>a;
cout<<a<<endl;

//rest of the code doesn't matter.

return 0;
}


Well as you said its "fine when i enter just one word without any spaces" I imagine you're not using an IDE like Dev-C++ which doesn't keep the console open after execution.

You're problem is to do without how you're getting your input - you're mixed buffered/unbfuffered input.

Change the line cin>>a; to getline(cin, a); and you program should work fine.
Topic archived. No new replies allowed.