string – put() prints duplicate lines in c-ThrowExceptions

Exception or error:

i was trying to run following code:

  #include<stdio.h>

void main()
{  
        int c;
        while((c=getchar()) !=EOF)
                        putchar(c);
                        }

i typed “hi” (or any other word) only once and when i press enter its print same thing again with a new line, no matter how large the line is

output

i dont know whats happening , the input stream is not buffered

How to solve:

Your terminal operates in line mode. So when you type the characters

hi

the program does not read them yet. They are just echoed to your terminal. When you press Enter, then all characters (hi plus a newline) are sent to the stdin of your program. These are read in character by character and then output. Thus, the text appears twice in your terminal.

If you want to change this, you have to put your terminal into character mode and switch local echo off. Libraries like ncurses can help you with that.

Leave a Reply

Your email address will not be published. Required fields are marked *