Thread: fgets hanging inside if statement

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool fgets hanging inside if statement

    TOPIC: fgets hanging inside if statement for string with spaces support


    what's wrong with this code? I believe fgets is hanging. I have tried this code in a separate source file and it works:

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
            FILE *fp;
            fp = fopen("groc.txt", "a+");
    
    
            char my_string[20];
    
    
            fgets(my_string, 20, stdin);
            fputs(my_string, fp);
    
    
            printf("You entered: ");
            puts(my_string);
    
    
            return 0;
    }


    This is the modification to the attached (grocery.c). I had scanf functions in here in my original code, this is an old project and i am trying to find out how to pull the old source code from github so I couldnt paste the old code. Anyway, I want support for a string with a space in it (that's why i'm now using fgets) but it's not working correctly inside the if statement but works in it's own separate file, above. i am trying to modify to the add_entry() function. Full source code attached (grocery.c)

    Attachment 15986
    Code:
        if (number == 1)
        {
            printf("Enter item to add (%d): ", number);
    
    
            fgets(fridge_string, 20, stdin);
            fputs(fridge_string, fp);
    
    
            printf("You added: ");
            puts(fridge_string);
    
    
            menu();
        }
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  3. If statement hanging...
    By tameeyore in forum C Programming
    Replies: 9
    Last Post: 10-08-2004, 10:34 PM
  4. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:33 PM
  5. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 01:26 AM

Tags for this Thread