Thread: fgets and readRestOfLine errors

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    fgets and readRestOfLine errors

    Hello guys, I am working on a menu, it keeps popping error and I have no idea why. Here's the code.
    -----------------------------------------------------------------------
    Code:
    int printMenu(void)
    {
        int option;
        char input[3];
    
    
        while((option != 3)||(option < 4)||(option > 0))
        {
            printf("Welcome\n");
            printf("---------------------\n");
            printf("1.Play \n2.Display Scores\n3.Quit\n");
            printf("Please enter your choice: ");
    
    
            fgets(input, 3, stdin);
    
    
            if (input[strlen(input) - 1] != '\n')
            {
                printf("Input was too long.\n");
                readRestOfLine();
            }
            else
            {
                input[strlen(input) - 1] = '\0';
            }
    
    
            switch (option)
            {
                case 1:
                    printf("Loading ...\n");
                    break;
                case 2:
                    printf("Loading ...\n");
                    break;
                case 3:
                    printf("Quitting...\n");
                    exit(0);
                    break;
                default:
                    printf("Invalid ! Please choose again.\n");
                    break;
            }
        }
    }
    
    void readRestOfLine()
    {
        int c;
    
    
        /*read until the end of the line or end-of-file*/
        while ((c = fgets(stdin)) != '\n' && c != EOF);
        
        /*clear the error and end-of-file flags*/
        clearerr(stdin);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum.

    You should post a complete program (with a main function) that we can compile and test. Also provide the errors you are getting.

    One thing I see - compare the "fgets()" call on line 15 to the one on line 54. Perhaps you means to use "getchar()" instead?

    Code:
    if (input[strlen(input) - 1] != '\n')
    If you get a zero length string, this will lead to a buffer underrun bug. See here for a better alternative on how to safely replace the newline with a null character, if present: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  2. How to use fgets
    By Nik Brkic in forum C Programming
    Replies: 11
    Last Post: 09-07-2013, 12:58 AM
  3. Replies: 3
    Last Post: 04-19-2013, 12:59 PM
  4. Fgets and Fprintf Errors?
    By Charak in forum C Programming
    Replies: 9
    Last Post: 03-23-2011, 09:06 AM
  5. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM

Tags for this Thread