Am I terminating my string?

Have written the following:

string a,b,c,d;

cout<<"write a string "<<endl;
cin>>a;
cout<<a.size();
cout<<"this is string a "<<a<<endl;

When I type "something something_after_a_space" it seems the string automatically terminates at the space character? Is this the nature of the string object because I haven't been able to find this written anywhere. If this is the case then why is it that when I use the getline member from the filestream class and pass the output to a string object that it works?

i.e if the string automatically terminates when it encounters a space character then why does something like:

ifstream myfile;

myfile.open("sometext.txt");
string a_line;
getline(myfile, a_line);

work even when the line contains spaces between characters?

Any help much appreciated
When I type "something something_after_a_space" it seems the string automatically terminates at the space character? Is this the nature of the string object because I haven't been able to find this written anywhere.


That's the nature of formatted string input via cin.


if the string automatically terminates when it encounters a space character then why does something like:getline(myfile, a_line); work even when the line contains spaces between characters?


Because that's the way unformatted line-oriented input works with cin.
Topic archived. No new replies allowed.