Get a new line as string

B

Bo Yang

HI,
How can I get a line of data into a string object. With cin.getline
() I can only put the line data into a char *. I want to the line data
be put directly into a string object. And the line may contain space/
tab, so a cin >> string, will not work.
Is there any convinient way to do this? Thanks

Regards!
Bo
 
N

Neelesh

HI,
   How can I get a line of data into a string object. With cin.getline
() I can only put the line data into a char *. I want to the line data
be put directly into a string object. And the line may contain space/
tab, so a cin >> string, will not work.
  Is there any convinient way to do this? Thanks

Regards!
Bo

std::getline can be used to read directly into a string. It is
available in two forms:

istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str ); //uses '\n' as
delimiter
 
R

Richard Herring

In message
Bo said:
HI,
How can I get a line of data into a string object. With cin.getline
() I can only put the line data into a char *. I want to the line data
be put directly into a string object. And the line may contain space/
tab, so a cin >> string, will not work.
Is there any convinient way to do this? Thanks

Use the non-member function std::getline.

#include <string>
....
std::string myString;
std::getline(std::cin, myString);
....
 
Æ

æ¨ æ³¢

In message


Use the non-member function std::getline.

#include <string>
...
std::string myString;
std::getline(std::cin, myString);

Thanks a lot!

Regards!
Bo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top