That the input is appearing on two seperate lines has nothing to do with your program; this is because, while typing the input, you pressed <Enter>
after 3
. The resulting newline was rendered by your terminal / console, not your program.
Using istream::operator>>( int )
will automatically skip leading whitespace. So your user could also write 3 5
on one line. std::cin>>a;
would consume the 3
, and std::cin>>b;
would skip the space and consume the 5
. Your terminal / console would look like this:
Enter two numbers to add
3 5
8
Note that this is completely unrelated to your program code, though. If the user presses <Enter>
between numbers, there is nothing you can do about it, short of taking over complete control of the terminal / console.
This can be done, using _getch()
on Windows, ncurses
on many other systems, or whatever the OS in question provides. You would be reading keypresses directly, without the terminal / console echoing what is entered. You would then be responsible for the echo, line editing etc.
That is a completely different question, though.