reading strings


I am using the standard string class, and I want to read in a whole sentence into a string.  What is the best way to do this?

if I do cin >> myString, it stops after each word (because of the spaces separating the word).  So how do I read the spaces into the string too?
LVL 2
VEngineerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nietodConnect With a Mentor Commented:
Use the global getline() procedure, like

getline(cin,MyString);
0
 
bbarnetteCommented:
You might try something like cin.getline(mystring,256) which would read in a line of up to 256 characters including white spaces.
0
 
yingchunliCommented:
You may try the foolowing code to read in a line of input (terminated by ENTER key):

if you are using char array:
{
...
char  s[MAX_CHARS];
cin.getline(s, MAX_CHARS, '\n');
...
}

if you are using CString:
{
CString myString;
...
char  s[MAX_CHARS];
cin.getline(s, MAX_CHARS, '\n');
myString=s;
...
}

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
nietodCommented:
The getline member procedure (cin.getline()) approach won't work to read a standard (STL) string.

To read an STL string you must use the GLOBAL getline() procedure.
0
 
VEngineerAuthor Commented:
Although your method works for the char* and CString representation of strings, I was asking about the new standard C++ string type, the one you get when you #include <string>.
0
 
VEngineerAuthor Commented:
nietod's on the right track, I think.  The parameters fit.  I am assuming that by default a \n character marks the end of the line for this function?
0
 
nietodCommented:
>>I am assuming that by default a \n character marks the
>> end of the line for this function
Yes, in this case.  As with the getline() member function it is overloaded.  There is a version that takes a 3rd parameter that is the terminator, thus you could do

getline(cin,MyString,'\n');

and it would be equivalent.  Note that the global getline is able to work with strings of any data type, like strings of integers or strings of some object type.  That is why it was made a global function, instead of a member function.


0
 
VEngineerAuthor Commented:

What is the reference that you use that explains all the details of standard library?
0
 
VEngineerAuthor Commented:

What is the reference that you use that explains all the details of standard library?
0
 
VEngineerAuthor Commented:

What is the reference that you use that explains all the details of standard library?
0
 
nietodCommented:
I ussually just the MS VC help.

But for a real reference, there is the Plauger book, something like "The Draft Standard C++ Softeare Template Library", (he basically wrote STL, if you didn't know)  A new version might be out by now that is updated for the final standard, but the differences between the draft and the standard are so minor that he wasn't sure he was going to write it.

Also Stroustrups "C++ Programming Language, 3rd edition."
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.