using getchar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ankit banerjee
    New Member
    • Dec 2011
    • 1

    using getchar

    I want to use getchar in a while loop such that it does not dont want to press enter everytime how is this possible
    for eg
    while((c=getcha r())!='\n')
    {
    printf("%c",c);
    }
    i want this variable c to be printed each time i press a character in run time and not pressing enter at all how is this possible i dont want my output at the end but simultaneously with pressing the character the character should be printed
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your program does not have control until the user presses enter. Any number of characters may be entered but the buffer is not available to you until the user presses enter.

    You would need to write your own keyboard driver. Or you could write in Windows code and look for message WM_KEYDOWN.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      stdin is commonly line buffered. This means that characters are collected (buffered) in the device driver until a newline is typed (for stdin). It sounds like you want stdin to be unbuffered. Take a look at stdio.h function setbuf and setvbuf.

      Comment

      • gautham786
        New Member
        • Dec 2011
        • 1

        #4
        Code:
        #include<stdio.h>
        #include<conio.h>
        void main()
        {
        Code removed...}
        
        }
        Last edited by weaknessforcats; Dec 7 '11, 04:01 PM. Reason: removed homework solution

        Comment

        Working...