473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to finish the input?

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
6 3251
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Boris Nikolaevich | last post by:
This is backwards of what I usually want--normally if you have a long-running ASP script, it's a good idea to check to see whether the client is still connected so you can cancel execution. ...
39
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have...
2
by: Jordan S | last post by:
I have written a small Console application that executes the following line, amongst other things: System.Diagnostics.Process.Start(currentWorkingDirectory + @"\backup_files.cmd"); My Console...
11
by: Peter Kirk | last post by:
Hi there I am looking at using a thread-pool, for example one written by Jon Skeet (http://www.yoda.arachsys.com/csharp/miscutil/). Can anyone tell me if this pool provides the possibility to...
0
by: mamod20 | last post by:
Please advise, I have the following example and want to know the best way to use $dbh->disconnect; and $sth->finish; -------------- $sql_host="localhost"; $sql_dataname = "database";...
6
by: iskeletor | last post by:
I didn't finish my program yet but in the input function there is a problem.When i enter E character while loop doesn't end. Why? Can anybody help me where is the error? From now, thanks ;) ...
1
by: Craig Coope | last post by:
I may be barking up the wrong tree here (maybe I can do all this in Excel) but... I have created an Excel sheet that lets me input job start and finish times and the amount of work done within...
2
by: elijahs | last post by:
This seems like an obvious one but it has me stumped. I have a complex form with a bunch of databound grids and textbox controls. There is a MainMenu control, and a toolbar control with...
1
by: sadpony | last post by:
Ok, so I wrote a program and it is supposed to let the user set the size of an array, input the numbers, find the subscript and value of the highest and lowest values, and then ask the user to input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.