Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting cin to read multiple words 1

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
How do i get cin to read all the words i type. Here's the code so far, this doesnt work right:

Code:
std::string input;
bool bQuit;
bool r;

void main(){
bQuit = 0;
r = console.openLogFile();
console.logMessage("Engine startup");
while (bQuit == 0){
std::cin>>std::noskipws;
std::cin>>input;
console.doCommand(input.c_str());
}
console.logMessage("Engine shutdown");
}

void quit(){
bQuit = 1;
}

Some of the commands that are typed into the console are like:

command var1 var2

But as the code is now, only the first word that i type is passed to the 'doCommand'. doCommand doesn't like this, it likes to have its arguments. As u can see, i tried the noskipws manipulator, but it didnt do nuthing. Thanx in advance!
 
I tried getline in place of cin but all it does is go into an infinite loop. It doesn't wait for the user to enter anything. This doesn't really solve the problem though, because i'm trying to store the user input into a string, which is why i chose cin to begin with because std::string can use the >> operator to extract stuff from an iostream. Any thoughts? I could use cin.getline i guess, but thats only if it works!
 
I don't see anywhere that the quit() function gets called in your code above. That would cause it to never end the while loop.
 
Try (then modify) this snippet:
Code:
char line[256];
int n = 0;
while (cin.getline(line,sizeof line))
{
  cout << ++n << "." << line << endl;
}
Alas, more natural
Code:
string line;
while (getline(cin,line))
{
// Process the line with user input
}
works badly in VC++ 6.0 (at 1st line from the console)...
Warning (see operator >> stream input definition):
Code:
string str;
cin >> str;
This code reads ONE WORD, not a line...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top