Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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?
0
VEngineer
Asked:
VEngineer
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
nietodCommented:
Use the global getline() procedure, like

getline(cin,MyString);
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now