|
-
December 27th, 2010, 02:30 PM
#1
How to input white space with type std::string?
Hi all,
When the following code is executed, we cannot input any string with white-space.
What if I actually want to input several words separated by white-spaces? How should I do that
Thanks in advance
Code:
string temp;
cin>>temp;
-
December 27th, 2010, 02:32 PM
#2
Re: How to input white space with type std::string?
Use the version of std::getline for std::string.
-
December 27th, 2010, 02:57 PM
#3
Re: How to input white space with type std::string?
Alternatively, you can also use the noskipws manipulator. I'd recommend the getline method though.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
December 27th, 2010, 03:03 PM
#4
Re: How to input white space with type std::string?
How to use the getline?
I tried
Code:
string temp;
cin.getline(temp,20);
Should I include a header file to make that work?
-
December 27th, 2010, 03:05 PM
#5
Re: How to input white space with type std::string?
OK, thanks!
I tried
And it worked.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|