Deepanshu Choudhary Deepanshu Choudhary - 1 year ago 92
C++ Question

How can I remain in a same line by pressing enter button in c++

I am working on a programe but now I am stick with a problem and the problem is I want to enter two numbers but with the cursor in same line when ever I enter any number and press enter it moves to the next line but I want it on the same line.please help me!!

Answer Source

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
Enter value of b

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 statement, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download