Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

reading strings

Posted on 1999-01-07
11
Medium Priority
?
201 Views
Last Modified: 2012-05-04

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
Comment
Question by:VEngineer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 

Expert Comment

by:bbarnette
ID: 1181725
You might try something like cin.getline(mystring,256) which would read in a line of up to 256 characters including white spaces.
0
 

Expert Comment

by:yingchunli
ID: 1181726
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
 
LVL 22

Expert Comment

by:nietod
ID: 1181727
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.

 
LVL 2

Author Comment

by:VEngineer
ID: 1181728
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
 
LVL 22

Accepted Solution

by:
nietod earned 160 total points
ID: 1181729
Use the global getline() procedure, like

getline(cin,MyString);
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1181730
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
 
LVL 22

Expert Comment

by:nietod
ID: 1181731
>>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
 
LVL 2

Author Comment

by:VEngineer
ID: 1181732

What is the reference that you use that explains all the details of standard library?
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1181733

What is the reference that you use that explains all the details of standard library?
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1181734

What is the reference that you use that explains all the details of standard library?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1181735
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question