Solved

reading strings

Posted on 1999-01-07
11
196 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 40 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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