Help with simple cin problem please.

S

Shane

So it's been a while since I've done anything in C++, but I thought I
could grab

cin >> input;

and it would go into string input, even if what's coming in happens to
be 3 words separated by spaces. I don't want to use c strings, I want
to use std::string.

So imagine the code

#include <iostream>

int main()
{
string input;

while(true)
{
cout << "Enter three words: ";
cin >> input;
cout << input;
}
}


So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.

What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Shane
 
M

Mark

On 7 Jul 2004 17:08:21 -0700, (e-mail address removed) (Shane) wrote:

[...]
So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.
And it should.
What I want to happen is for input to store one string with three
words, etc.

Can someone help?
Have a look at getline

std::string input;
std::getline(std::cin, input);
std::cout << input << std::endl;

or
copy(istream_iterator<string>(cin),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));


Mark
 
M

Matthew Del Buono

Shane said:
So it's been a while since I've done anything in C++, but I thought I
could grab

cin >> input;

and it would go into string input, even if what's coming in happens to
be 3 words separated by spaces. I don't want to use c strings, I want
to use std::string.

So imagine the code

#include <iostream>

int main()
{
string input;

while(true)
{
cout << "Enter three words: ";
cin >> input;
cout << input;
}
}


So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.

What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Shane

Instead of using cin, you might consider using getline:

getline(cin, input, ' ');

Note: This will only accept words separated by a space. If there is a
newline in there, it'll be lumped in with the word, so be careful.

-- Matt
 
S

Shane

Mark said:
On 7 Jul 2004 17:08:21 -0700, (e-mail address removed) (Shane) wrote:

[...]
So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.
And it should.
What I want to happen is for input to store one string with three
words, etc.

Can someone help?
Have a look at getline

std::string input;
std::getline(std::cin, input);
std::cout << input << std::endl;

or
copy(istream_iterator<string>(cin),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));


Mark


Thanks for the point in the right direction. That's exactly what I needed.

Shane
 

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
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top