Trouble understanding extracting w/ istringstream.

I'm trying to parse user input into a command and a string to be operated on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

int main (){
	std::cout << "input-> ";
	std::string input, command, line;
	std::cin >> input;
	std::istringstream inString(input);

	inString >> command >> line;
	std::cout << command << line;

	return 0;
}


With the input "hello world", I get the output, "hello", but not the second word, "world". To me, this looks contrary to every example using istringstream that I've seen... I would expect that "equation" would come out as "world".

What have I done wrong here?

Thanks!
The operator >> reads only words separated by white spaces when it is used for entering strings.
So sfter the statement

std::cin >> input;

if you entered "hello world" variable input will contain only "hello".
If you want to read all input data before the new line character you should use standard function std::getline( std::cin, input ). instead of the operator >>.
Last edited on
^learned something new, thanks much!
Topic archived. No new replies allowed.