Thread: fgets hanging inside if statement

  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

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Be careful when mixing scanf() with fgets(), the scanf() function leaves the end of line character in the input buffer which fgets() will take as the end of it's input.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Also be careful not to use recursion where it's not warranted. menu() calls write_func(), which calls add_entry() or remove_entry(), which then calls menu(). Instead of calling menu(), just use a return statement. It'll return (hence the name) back to menu() automatically, since the functions are called by menu().

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool

    Quote Originally Posted by jimblumberg View Post
    Be careful when mixing scanf() with fgets(), the scanf() function leaves the end of line character in the input buffer which fgets() will take as the end of it's input.

    this cleared the buffer

    Code:
    while ((getchar()) != '\n');
    Thanks, need to figure out working with fprintf and fputs now when writing to the text file

  5. #5
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool

    Quote Originally Posted by christop View Post
    Also be careful not to use recursion where it's not warranted. menu() calls write_func(), which calls add_entry() or remove_entry(), which then calls menu(). Instead of calling menu(), just use a return statement. It'll return (hence the name) back to menu() automatically, since the functions are called by menu().
    this program was written 13 months ago when I had very little C experience and I have resumed the project, will work on the "infinite recursion" problem. This is the working code for the fgets and outputting it to the text file. This is, therefore, spaghetti code.

    Code:
     if (number == 1)	{
    		while ((getchar()) != '\n');
    
    
    		printf("Enter item to add (%d): ", number);
    
    
    		fgets(fridge_string, 20, stdin);
    
    
    		printf("You entered: ");
    		puts(fridge_string);
    		
    		fprintf(fp, "%s", fridge_string);   // print to text file
    
    
    		fclose (fp);
    
    
    		return;
    	}

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    this cleared the buffer
    Be careful with that, if there is no newline character in the input buffer that "clear" routine will hang, waiting for the newline.

    this program was written 13 months ago when I had very little C experience and I have resumed the project, will work on the "infinite recursion" problem.
    Have you considered just starting over?

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