-1

In the below program:

#include <iostream>
using namespace std;

int main()
{
    char ch;
    while (cin >> ch){
        cout << ch; 
    }
   return 0;
}

input : abcEnter
output : abc

Whatever I type on the keyboard gets printed only when I press Enter i.e. new line.

Why is not each character being printed simultaneously? Which statement here takes the newline as an input and prints all the characters printed till now?

user1718009
  • 303
  • 3
  • 7

1 Answers1

2

Your terminal doesn't send every new character as it's pressed; it waits for you to provide a whole line then sends it all in one go.

In the Linux world, this is called canonical mode.

If you want the result of your keystrokes to be immediately sent to whatever your terminal is connected to, turn off canonical mode.

I cannot tell you how to do that, however, as you did not say what your terminal is.

It is sometimes possible for the program itself to request that the terminal not use canonical mode (e.g. curses applications have to do this), but the manner in which this is achieved depends on your operating system — and, again, what your terminal is.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055