By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

how to finish the input?

P: n/a
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I press
Enter key, but it didn't terminate input.

Sep 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I press
Enter key, but it didn't terminate input.
The "while" construct is for loops. You don't need a loop in this
program. Why are you using "while"?

Best regards,

Tom

Sep 20 '06 #2

P: n/a
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I press
Enter key, but it didn't terminate input.
This depends on the platform you are using. Under Windows you have press
Ctrl + Z, under Unix Ctrl + D to issue an EOF.

Regards,
Stuart
Sep 20 '06 #3

P: n/a
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I press
Enter key, but it didn't terminate input.

That because newline is just another white space to >>. My
recommendation is to use some sort of counting loop.
void GetStrings(std::vector<std::string&v, int n)
{
std::string s;
char c;

std::cout << "";

for (int i = 0; i < n; i++)
{

std::cin >s;
v.push_back(s);
}

std::cin.get(c); // eats the newline hanging out
}

Brian
Sep 20 '06 #4

P: n/a
Thomas Tutone wrote:
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I
press Enter key, but it didn't terminate input.

The "while" construct is for loops. You don't need a loop in this
program. Why are you using "while"?
Yes, he does. He wants to read two words out of one input line.

Brian
Sep 20 '06 #5

P: n/a
Stuart Redmann wrote:
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I
press Enter key, but it didn't terminate input.

This depends on the platform you are using. Under Windows you have
press Ctrl + Z, under Unix Ctrl + D to issue an EOF.

This sort of answers the OP's question, but a platform-independent
solution is probably better.


Brian
Sep 20 '06 #6

P: n/a
Thomas Tutone wrote:
asdf wrote:
string text_word;
while (cin>>text_word)
vector1.push_back(text_word);

I just want to input two strings, how to terminate the input ? I
press Enter key, but it didn't terminate input.

The "while" construct is for loops. You don't need a loop in this
program. Why are you using "while"?

I had another reply to your message that I canceled, however it may
show up anyway. This is a more accurate reply.

A loop is one way of doing it, I presented one in another message.
Obviously another way is to have repeated code. That's not the most
efficient way in general, although for two it probably doesn't make
that much difference.


Brian
Sep 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.