[Solved] How to take multiple inputs in the same line in C++?


You can do this simply by cascading the the cin operator. If you write a code in this way:

int a,b;
cout << "Enter value of a" << endl;
cin >> a;
cout << "Enter value of b" << endl;
cin >> b;

then the program execution would be in this way:

Enter value of a
10
Enter value of b
20

But to do this in a single line, you can write the code in this way:

cout << "Enter the values of a and b" << endl;
cin >> a >> b; //cascading the cin operator

The program execution now goes thus:

Enter the values of a and b
10 20

If you enter both values this way (separating them with a space), then it works the way you want it to – being in the same line.
Also, in the first snippet, if you remove the endl keyword, you can also have it all in one line but I don’t think that’s what you want.

Also see: CASCADING OF I/O OPERATORS | easyprograming.

2

solved How to take multiple inputs in the same line in C++?