Thread: getchar() problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    getchar() problem

    hey. when I use (ans!='d') the printf("sum\n") prints the line twice and then the getchar() function is beeing used.

    Code:
    #include <stdio.h>
    main()
     {
       char ans;
       ans= getchar();
       while(ans!='d')
       {
         printf("sum\n");
         ans= getchar();
       }
     }
    on the other hand when I use (ans=='d') then the second ans= getchar() function doesn't work. the program just ends.
    what am I not doing correct?

    dh

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >main()
    Code:
    int main ( void )
    is the correct definition of main without arguments.

    >char ans;
    Despite the name, getchar actually returns an int so that it can properly handle EOF.

    >what am I not doing correct?
    I don't know. What input are you giving the program and what did you expect to happen?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    reply

    what I like to do is to have one user input. and if that user input is the condition used with the while loop the loop should be run else not. then when the loop is almost finished (in the end of the loop) have another user input to ask to repeat the same thing (the same loop)

    (the example program is just to make it simple)

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    So you basicly want somthing like this:

    Code:
    printf("Enter a number: ");
    scanf("%d", &num);
    
    while ( num != 100 )
    {
       printf("No, give me a ton: ");
       scanf("%d", &num);
    }
    That may not ne what you want, but its along the same lines
    Double Helix STL

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    void clear_buffer(void)
    {
        int ch;
        
        while((ch=getchar()) != '\n' && ch != EOF);
    }
    
    int main()
    {
       int ans;
       
       while((ans=getchar()) != 'd')
       {
         printf("sum\n");
         clear_buffer();
       }   
       
       getchar();
       return 0;
    }
    hope this will solve the problem

    sshaish2005

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    hi,

    you got to understand one thing here, getchar accepts a character from the standard input device .i.e. keyboard, when u press a character it is stored in 'ans' first, then you press enter key which is also a character again.
    This is what lets your program print the statement twice, to rectify this you need not change your whole program like other people suggested, but just add a line in your present program saying ' fflush(stdin); ' immediately after the getchar() line.
    This will solve your problem.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    program saying ' fflush(stdin);
    Why fflush(stdin) is wrong
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Hi vart,
    I mean the program should be something like this
    Code:
    void main()
    {
    char ans;
    ans=getchar();
    fflush(stdin);
    while (ans!='d')
    {
    printf("sum\n");
    ans=getchar();
    fflush(stdin);
    }
    }
    Vart, actually fflush() refreshs the buffer so, the problem will not persist.
    Am I wrong Vart?

    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    on some systems it will work, on some - will not...
    so - the FAQ provides a portable way to fflush input stream:
    FAQ > How do I... (Level 2) > Flush the input buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    hey all. thank you for many replies.

    SWGH: your answer works using an int variable, but not using a char variable

    ssharish2005 and vart: your answers works just fine. thank you

    dh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM