I am working on a program but now I am stuck with a problem and the problem is I want to enter two numbers but with the cursor in same line. Whenever I enter any number and press enter it moves to the next line but I want it on the same line. How do I take multiple inputs in the same line?
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 .
For two variable a
and b
, You can write the code in this way,
cout << "Enter the values of a and b: ";
cin >> a >> b;
The program will be executed as follows,
Enter the values of a and b: 5 10
cout << "Enter the values of a and b" << endl;
cin >> a >> b;
Program will execute in this format now
Enter the values of a and b
10 20
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.