2 integers on the same line

i want to know how to enter 2 numbers(the inputs) on the same line and separated by space like this
the output is:
Enter 2 integers: 3 5
but when i use this code cin>>a>>b; it always turn out like this:
Enter 2 integers : 3
5
is there anyway to make the second input on the same line with the first one as the same as the first exp .

Simply enter both 3 and 5 first and then press enter.
closed account (1vf9z8AR)
cin>>a;cout<<"\t";cin>>b;
@suyashsing234
That won't work the way it looks. Just

cin >> a >> b;

The user is free to put any kind of whitespace between the two numbers he wishes.
closed account (1vf9z8AR)
@Duthomhas
my method works for me but not for you.
your method works for you but not for the OP.

what is happening? :)
different methods same language lol.
I think you are making assertions you cannot back up.

Your method, OP can enter both numbers at once, and you get a weird tab in a funny spot (user input in bold):
3 4<ENTER>
<TAB>


Or OP can press ENTER after every number and get a weird tab in a funny spot:
3<ENTER>
<TAB>4<ENTER>


Both are confusing and distracting to the user, who will rightly ask why is this tab getting printed here during input?

The one rule you should remember when getting input is this:

    The user will always press ENTER after every input prompt.

OP prompts for two items, so you do not know whether he will press ENTER once per number or once for both. Write your code to accept it either way.

If it otherwise matters, then make two prompts:

1
2
3
4
5
6
7
  cout << "Please enter the first number: ";
  cin >> a;
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  cout << "Please enter the second number: ";
  cin >> b;
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

tl;dr: don't do things that will seem weird to the user and don’t do things that force a user to be present.
Last edited on
closed account (1vf9z8AR)
yes.Your second method is best as one is clear that he/she is entering another number
Topic archived. No new replies allowed.