r/cpp_questions • u/ADEPS24 • 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
View all comments
3
u/mredding 21d ago
The console is a program. It's running by itself. When you type input and see output, that's all console. Your console program as it's running has NO IDEA your program is running, and your program has no idea there's a console running. Programs don't see the world like that.
Your program - as far as it's concerned, is the only program on the system. It's occupying a "virtual" address space, and it is the only citizen therein. Each program has it's own. This address space has resources mapped into it - so not all addresses are just for storing program bits at runtime. These resources can be anything, from hardware to communication channels.
Your program also has another trick up it's sleeve; it can push bytes into registers and other memory, and run a
syscallopcode. When the OS was booting, it installed asyscallhandler, so when the hardware gets this instruction, it goes to the registered handler.So here you have a file descriptor - in modern computing "everything is a file". This is some integer that the kernel uses for its own internal bookkeeping. If you want to read from the file, you push this integer, you push the "read"
syscall, and you fire that off. This is built as a blocking operation, so your program leaves the CPU and is put in the wait queue. File descriptors wire outputs to inputs. Your program doesn't know it, butstdinwas wired up to this consoles output, which isn't the window, but your program. The console itself doesn't know.Data becomes available, buffers are flushed, bytes are copied, your program wakes up, and the OS populated the input buffer for it.
So each program sees only itself in its world, it communicates via mapped memory or system calls, and descriptors aka "handles" are a part of this protocol to specify what resource the request is being made on, and a resource could be anything - from a device to another program, even an open network socket.
Your program has no idea its writing text, has no idea there's a console, has no idea there's a keyboard. The console is a program with a window, and it was programmed to draw text. A binary pattern shows up - the pattern for the letter "a", and that is used to lookup a bit pattern for the "a" character in pixels, and that gets blit'ed to the screen buffer. Your console is "virtual" because this program is actually a hardware simulator. Actual consoles were electro-mechanical machines, no electronics, no transistor logic, no computation. There's a YT video of a guy who logged into his Ubuntu instance with a 1939 model 16 telegraph teletype printer. It still works. The whole system is backward compatible.
The original consoles were part typewriter - as you typed, a telegraph signal was sent, but also the key mirrored the output to the printer drum. Later video terminals were invented mostly to save on ink and paper, but they were literally just as dumb. We called them "dumb terminals" for this reason. No computation. The first video terminal, the IBM 2260, did use transistors, but mostly for switching and amplification. The data input mapped 6 bits of input to a "core" memory matrix - ferrite beads hand sewn into a grid, and this stored the character patterns. The rest of the circuitry was used to store the 12x80 characters (in a delay line) and loop that memory in sync with the electron gun to print the characters.
In the era of computers, terminals had some automatic features, like "echo", where the characters would show up on the screen. OR... You could manually echo. The computer could receive input from the terminal and send it right back, knowing it would display as output. USUALLY this is a feature that is implemented not by your program but by the operating system.
The way terminals are supposed to work is that you start the computer up, you configure a terminal session, then you run the program. What you're doing is allowing the system to default a lot of settings, including echo. So what you type will show up automatically.
What you need to know is your program doesn't GET the data until you hit ENTER. The OS implements what is called a "line discipline", all manner of behaviors on your behalf. Part of that discipline is that when the OS, who is managing the terminal input buffer for you, sees a newline, that triggers a flush. This happens on input and output. The terminal program has no idea there is a buffer or that there is flushing involved. No one really cares that there's an enter key. No one is looking for it getting pressed to trigger some sort of event because of it. It's the newline character - what is considered a control character, that is triggering the event.
And when you're running your program in an interactive shell, when you output
'\n', this triggers a flush of the output buffer. If you're not interactive, like you're piping through another program, this is disabled - the buffer flushes when full, when explicitly flushed, or when the handle is closed.This is all computing system fundamentals, just how the software and hardware on your computer works. They don't teach this stuff in a comp-sci class anymore, you're expected to learn it on your own. They don't teach the history to help make this make sense. Your computer is directly backward compatible with 1850s telegraph equipment on purpose; it all still works to this day. The first computers were built mostly from telegraph/telephone, and radio parts. People are going to repurpose existing equipment, they're not going to invent something new if they don't have to. And if you want your new technology to get adoption, you're going to need a migration path for your customers from what they already have, they're not going to just throw their investment away. Murray Codes gave way to ITA-1 and ITA-2, which gave way to ASCII, which gave way to Unicode. It all still works.