Thread: Possible fgets issue

  1. #1
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19

    Question Possible fgets issue

    To be honest I am not sure this is a fgets issue. It may be an issue with running a fgets within a function or something I am just not aware of. I did attempt to run the contents of the function nameRequest() within the main() and commented everything out. Doing this did work, and gave the desired output. I appreciate any and all help. I will explain the details below:

    Desired Outcome:


    The program I have written is suppose to simply ask your name, then return via printf which presents the input given using the previous fgets.

    Current Outcome:

    What I am running into, is instead of asking your name then, allowing you to enter it, the program skips the fgets to the final printf and ends.

    Here is what I get:

    Compiler:
    Welcome to the Game!
    +=============+
    |1. Create New |
    |2. Exit Game |
    +=============+
    Your Selection: 1
    Please enter a character name:
    Is , correct?
    Code:
    #include <stdio.h>#include <string.h>
    
    
    #define MAX_AMOUNT 15
    
    
    void nameRequest(void);
    void mainMenu(void);
    
    
    
    
    int main(int argc, const char * argv[])
    {
    mainMenu();
        
        return 0;
    }
    
    
    void mainMenu(void){
        int choice = 0;
        
        for (choice = 0; choice < 2 || choice > 2;) {
    printf("Welcome to the Game!\n");
    printf("+=============+\n");
    printf("|1. Create New|\n");
    printf("|2. Exit Game |\n");
    printf("+=============+\n");
    printf("Your Selection: ");
            scanf("%d", &choice);
            
            if (choice == 1) {
                nameRequest();
                choice = 2;
            }
            else if (choice == 2) {
                choice = 2;
            }
            else {
    printf("\nError! You must enter in one of the above numbers!\n");
            }
        }
    }
    
    
    void nameRequest(void){
        
        char toonName[MAX_AMOUNT];
        int length = 0;
    printf("Please enter a character name: ");
        fgets(toonName, MAX_AMOUNT, stdin);
        length = strlen(toonName);
        toonName[length - 1] = 0;
    printf("\nIs %s, correct?", toonName);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's often a bad idea to mix input routines, particularly where scanf is involved. You ask for an integer with "%d", so that's what scanf reads. But when it gets to the newline (remember, you pressed enter after you typed the integer), it stops, so there is a '\n' left in the buffer. When you call nameRequest, the fgets in there sees the newline and says "ahh, you typed an empty string". Read this article on how to FAQ > Flush the input buffer - Cprogramming.com.

    Your method of trimming the newline is a problem if there is no newline (which is possible if the user uses EOF to send input instead of a newline). If they enter an empty line, you have a buffer underrun, and you access toonName[-1], an invalid index. If they enter some data, but no newline, you chop off the last character, which could be part of the name. You should use a better method of trimming the newline that fgets leaves, such as the strchr method presented here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

  3. #3
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    Could I essentially create a flush Input buffer function and call it each time, text is required to be taken in?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    2
    after the scanf() you should clear the keyboard (stdin) buffer. You can do that with fflush(stdin). Use it after the scanf function and this should work. Hope it helps....

  5. #5
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    Quote Originally Posted by hkshaw1990 View Post
    after the scanf() you should clear the keyboard (stdin) buffer. You can do that with fflush(stdin). Use it after the scanf function and this should work. Hope it helps....
    Hey thanks for the idea. I did try it and no luck. However it got me thinking, and I finally remembered the function fgetc(stdin). I tried it and now it works. I did however follow up with your suggestion at the end for future inputs.

    Code:
    void nameRequest(void){
        
        char toonName[MAX_AMOUNT];
        int length = 0;
        
        printf("Please enter a character name: ");
        fgetc(stdin);
        fgets(toonName, MAX_AMOUNT, stdin);
        length = strlen(toonName);
        toonName[length - 1] = 0;
        printf("\nIs %s, correct?", toonName);
        fflush(stdin);
    }
    
    Thank you both for your help!

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well, fflush(stdin) actually gives undefined behaviour.
    Read this: FAQ > Why fflush(stdin) is wrong - Cprogramming.com

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    there's a tutorial on fgets() on this site which clarifies the situation and gives a solution.
    just type into the search engine

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Layvian View Post
    Could I essentially create a flush Input buffer function and call it each time, text is required to be taken in?
    Yes, and it would be a very good idea. Every time you find yourself repeating a piece of code, you should strongly consider putting it in a function.

    Note, just calling fgetc is not sufficient. If the user accidentally hits a space or any non-digit after the number, it wont work, it only reads one character (the space). If the user uses EOF to end their input, toonName will be missing the first letter. Use the method in the first link I gave you in post #2 (that red text is a link).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Facing issue with fgets in while loop
    By pingloo02 in forum C Programming
    Replies: 4
    Last Post: 04-17-2012, 06:14 AM
  2. Storing using fgets issue
    By ccc123 in forum C Programming
    Replies: 3
    Last Post: 05-30-2011, 11:41 AM
  3. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 08:15 PM
  4. fgets issue
    By Fox101 in forum C Programming
    Replies: 2
    Last Post: 05-05-2008, 10:11 PM
  5. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 11:26 AM