If setw sets "minimum field width", why does it limit input or output?

If I write: 120 450 340 as input, I only get 120 back. I don't get it.

1
2
3
4
5
6
7
8
9
10
11
12
13
  int main()
{
	int x{};

	cout << "Enter nr: " << endl;

	cin >> setw(1) >> x;

	cout << endl << x << endl;

	system("pause");

}
The setw() is irrelevant here. (For an input stream it is only relevant for a c-string or string).

You asked to input ONE integer. So, regardless of how many you put on the line, that is all you got. Stream extraction stopped at the first whitespace (here, just a space).

The numbers 450 and 340 are, however, still left in the stream. Further cin >> statements will extract them, one by one.
Last edited on
Topic archived. No new replies allowed.