a question about fflushing

This is a discussion on a question about fflushing within the C Programming forums, part of the General Programming Boards category; Hi. I'm doing this loop: Code: char answer[3]; do { printf("Enter (y/n): "); fgets(answer, sizeof(answer), stdin); } while (!(answer[0] == ...

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    a question about fflushing

    Hi. I'm doing this loop:

    Code:
         char answer[3];
    
           do {
                  printf("Enter (y/n): ");
                  fgets(answer, sizeof(answer), stdin);
           } while (!(answer[0] == 'y' && answer[1] == '\0') &&
                         !(answer[0] == 'n' && answer[1] == '\0'));
    It keeps printing:

    Enter (y/n): Enter (y/n):

    twice after each typo someone makes.

    I addes fflush() after the printf() since I'm not using the newline, and I still
    get the same double output. What am I doing wrong?


    Thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > answer[0] == 'y' && answer[1] == '\0'
    This will never be true.
    Your string at the very least is going to contain "y\n\0"

    If there is room in the buffer, fgets() will store the newline, it not, it will leave the newline on the input stream for the next call.
    There is always a \0 in the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Sorry that was a typo:

    I meant, and in my program had:

    [code]

    while(!(answer[0] == 'y' && answer[1] == '\n') &&
    !(answer[0] == 'n' && answer[1] == '\n'));


    My question is about the double printing on typos within
    the loop

    Enter (y/n): Enter(y/n):

    after every mistake the user makes:
    Instead of

    Enter (y/n): s
    Enter (y/n):

    Thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    If the input doesn't fit (you only allow for one character of actual visible type), or there are previous calls to getchar() or scanf() in the code, then you'll see the effect you describe.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21