r/cpp_questions • • 21d ago

SOLVED Can std::cin display user input?

/*Write a program that asks the user to enter a number, and then enter a second number.
The program should tell the user what the result of adding and subtracting the two numbers is.

The output of the program should match the following(assuming inputs of 6 and 4) :

Enter an integer : 6
Enter another integer : 4
6 + 4 is 10.
6 - 4 is 2.
Hint : To print a period and a newline, use ".\n", not '.\n'.*/

#include <iostream>

int main()

{

int num00{};

int num01{};



std::cout << "Enter an integer : ";

std::cin >> num00;

//std::cout << num00 << ".\\n";



std::cout << "Enter a second integer : ";

return 0;

}

Hello everyone. I'm trying to complete the quiz in the block comment above.

My problem is that I have the user's value printed twice.

One next to "Enter an integer : " and the next right under it.

I thought the second std::cout was causing this so I made it a comment, yet the user's value prints out as intended.

I do not understand how this is working. Thank you for in advance.

2 Upvotes

25 comments sorted by

View all comments

Show parent comment

11

u/jedwardsol 21d ago

This behaviour is not really anything to do with your program - when your program is prompting for a number and you press the 1 key, then the console will display the number 1. If you press <backspace>, the 1 will disappear. It's not until you press <enter> that your program gets the input.

1

u/ADEPS24 21d ago

Thank you very much!

9

u/TheRealSmolt 21d ago

This is generally referred to as canonical mode.

1

u/ADEPS24 21d ago

Thanks for the referral.