Solved

reading strings

Posted on 1999-01-07
11
199 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

691 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