Integer cin

Hello,

for example i have this code
1
2
3
4
5
6
7
  int a, b;

  cout << "enter a number " << endl;
  cin >> a;

  cout << "enter a number " << endl;
  cin >> b;


if i type in the first question for example

1 3

it takes 3 as the next answer. So my question is how can i make my programm take only numbers without spaces in it?
You want to type "13" and set a=1 and b=3?
no no,

if someone types 1 and after the space an other number it takes the number after the space as the next input and i want to prevent that.
closed account (E0p9LyTq)
Add #include <limits> with your other include(s).

After std::cin >> a; add:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

That will extract and discard any remaining characters in std::cin's buffer.
Oh, so

1 3
2


should result in a=1 and b=2.
Topic archived. No new replies allowed.