Thread: fflushing input and why do I have to hit enter key twice? Please, help fix this.

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    5

    fflushing input and why do I have to hit enter key twice? Please, help fix this.

    Hello everyone!! I am a C programming newbie!
    Any help will be appreciated!

    /* I wrote a program that reads user input string using fgets() function. After this, program will ask if the user
    wants to save the string to file or does the user want to change the string or quit without saving. If user
    wants to change the string this must be made possible by asking for the string again. After this, menu will
    be printed again printing the choices available.

    I had to flush stdin before getting changing input!
    However, problem is "Why do I have to hit return key 2 times, when I choose 2 and input string again."
    You will see my problem when you compile and run this.
    Thank you.
    (I use code block 13.12 with windows 8.1 laptop) */

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 50
    
    
    void printMenu();
    
    
    int main(void)
    {
        char input[MAX], choice, flush;
        FILE *fp;
        int i;
        printf("Input a string: ");
        fgets(input, MAX, stdin);
        fflush(stdin);
        printMenu();
        do
        {
            choice=getchar();
            if (choice == '1')
            {
                fp=fopen("e27save.txt", "w");
                fputs(input, fp);
                fclose(fp);
                break;
            }
            else if (choice == '2')
            {
                while ((flush = getchar()) != '\n' && flush != EOF); // flushing stdin, or use "fflush(stdin);"
                printf("\nInput a string: ");
                fgets(input, MAX, stdin);
            }
            else if (choice == '3')
            {
                break;
            } else
            {
                printf("Please choose a valid option\n");
                printMenu();
            }
        } while(1);
    
    
        return 0;
    }
    
    
    void printMenu()
    {
        printf("1. Save the file, 2. Change the string, 3. Quit without saving: ");
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    fflush(stdin) is wrong. The standard explicitly states that calling fflush on an input stream, or a read-write stream whose last operation was a read, results in undefined behavior. Why are you calling fflush(stdin)? Who told you to do it that way?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I agree with what Elkvis says.

    About the problem you're asking about ... perhaps you meant to call "printMenu()" at the top of the loop, instead of before the loop?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    The following code is a the way to flush stdin correctly:

    Code:
    int ch = 0;
    while ((ch = getchar()) != '\n' && ch != EOF);

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    Thank you rstanley, I already did it but same problem... Elkvis I used fflush to fix a bigger bug. If I dont use it, it prints multiple error messages. I can use rstanley's code instead of fflush

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    FYI, fflush on line 16 can be omitted(it is not necessary)
    the problematic part is line 30(choice 2)

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    I finally fixed it !! the second enter was because of the getchar, and I moved it around!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 50
    
    
    void printMenu();
    
    
    int main(void)
    {
        char input[MAX], choice, flush;
        FILE *fp;
        int i;
        printf("Input a string: ");
        fgets(input, MAX, stdin);
        printMenu();
    
    
        choice=getchar();
        do
        {
            if (choice == '1')
            {
                fp=fopen("e27save.txt", "w");
                fputs(input, fp);
                fclose(fp);
                break;
            }
            else if (choice == '2')
            {
                while ((flush = getchar()) != '\n' && flush != EOF); // flushing stdin, or use "fflush(stdin);"
                printf("\nInput a string: ");
                fgets(input, MAX, stdin);
                printMenu();
                choice=getchar();
            }
            else if (choice == '3')
            {
                break;
            } else
            {
                printf("Please choose a valid option!\n");
                printMenu();
                choice=getchar();
            }
        } while(1);
    
    
        return 0;
    }
    
    
    void printMenu()
    {
        printf("1. Save the file, 2. Change the string, 3. Quit without saving: ");
    }

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Shouldn't the flush of stdin be located in the else section?

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    yes I moved it to
    else if (choice == 2) section.
    And it works for me.
    Thank you !

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    I looked at it too quickly.

    It should also be in the else section.

    After entering the first string, at the prompt, type in a string of characters, but not 1, 2, or 3, and you ill see the other problem you have.

    In the option 2 section, after the fgets() call, you should call printmenu() again.

    Turn on your warning level to see the other warnings. (Int i)

    If a function take no arguments, the it should be declared/defined using the void keyword. (printmenu)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how,without a ENTER to finish the string-input?
    By procs in forum C Programming
    Replies: 2
    Last Post: 05-05-2010, 08:26 AM
  2. user input pre-enter string?
    By mikeyp in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2009, 02:59 PM
  3. input function without a need to press Enter?
    By chao06 in forum C Programming
    Replies: 1
    Last Post: 06-19-2009, 03:18 PM
  4. a question about fflushing
    By movl0x1 in forum C Programming
    Replies: 3
    Last Post: 07-24-2007, 05:58 AM
  5. Getting input without the user having to press Enter...
    By Ranedhel in forum C++ Programming
    Replies: 18
    Last Post: 07-16-2003, 05:36 PM

Tags for this Thread