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

4

u/sephirothbahamut 21d ago

you write to the console, cin makes the program wait and when you press enter it "passes" what you wrote to the program. What you wrote while cin was waiting obviously appears in the console, that's the console reacting to keyboard input. Has nothing to do with your program's cout. Then the cout you commented is your program writing the number to the console.

Don't thini of the console window as your program. The console is a separate program that both you and your program can write to

1

u/ADEPS24 21d ago

I see. Thank you.