Thread: fgets won't stop taking input on Mac

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    125

    fgets won't stop taking input on Mac

    Haven't touched C in a while. I don't remember having issues with fgets() but right now, I have this:




    int age;
    char name[50];

    printf("Enter your name\n");
    fgets(name, 50, stdin);
    scanf("%d", &age);



    When prompted for my name, I enter "test" without quotation marks obviously. When I press enter, the terminal just goes down a line... Over and over and over and it never stops and progresses past this point.

    I remember having this issue sometimes with scanf and being able to use getchar() after or specify a "%s\n" in the call but not with fgets...

    This is on MacOS High Sierra and I'm using gcc-8. What have I forgotten? Thanks.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Pro tip: use [code][/code] tags for code. It will display in a fixed-width font and automatically do syntax highlighting (so paste your code as plain text). This will be much more readable.

    As for your problem, it has to do with scanf's behavior. Using %d tells scanf to "listen" for an integer; by definition, %d skips any white space, including new lines. So all those enter presses are ignored by scanf. You'll get a different behavior if you try to type in text after entering your name. And if you enter an integer, it will read it.

    Try running this code. Play with different values for age, including floating point numbers and text (basically anything but -1 will work).
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int age = -1;
        char name[50];
    
    
        printf("Enter your name\n");
        fgets(name, 50, stdin);
        printf("Enter your age\n");
        scanf("%d", &age);
        printf("Hello %s, you are %d years old.\n", name, age);
    
    
        return 0;
    }
    What do you notice about the name? What is printed for age if you don't enter a valid integer?

    By the way, the getchar/newline issue you're remembering is probably the one explaned here: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    EDIT
    Read up on scanf documentation here for more info: scanf(3): input format conversion - Linux man page

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    125
    Funny thing is the real bug here was that I apparently was exhausted when I was working on this because I failed to realize that I never prompted the user for the age which would have made this whole thing a lot more clear...

    Thank you for your explanation!
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with taking input from a file
    By babe20042004 in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2009, 08:25 AM
  2. Int Array - Stop taking values at [zero]
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-04-2008, 12:54 AM
  3. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  4. Taking input in C
    By GUIPenguin in forum C Programming
    Replies: 1
    Last Post: 04-12-2006, 01:53 PM
  5. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM

Tags for this Thread