Thread: fgets after scanf, skips over fgets?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    7

    fgets after scanf, skips over fgets?

    Hi every one I am wrting a little bs program just to practice c and this is a calorie counter that will get bigger but I am just starting with it to get the practice. Here is the code and I sperated the prolem code below when I get a value via scanf it skips the next fget.
    Code:
    #include<stdio.h>
    #include<stdbool.h>
    #include <string.h>
        int maxAday = 2500;
        int todaysCount = 0;
        bool stop = false;
        bool firstrun;
        main() 
            {
                int dowhat;
                char ate[100];
                int calIn = 0; 
                char yesno[10];
    
                printf("What would you like to do? \n");
                //printf("Add Calories use 0: \n Calculate Day 'cd'\n Calculate Month 'cm'\n");
                printf("Type 0 to get started.");            
                scanf("%d", &dowhat);        
                    switch (dowhat)
                    {
        
                    case 0:
                        do
                        {
                            if (firstrun)
                            {
                            printf("Type what you ate if or manual to input the amount of calories you can also type help for a list of supported options.\n");                
                            }
                            fgets(ate,100,stdin);                        
                            *(&todaysCount) += returncals(ate);
            
                            if (firstrun)
                            {                        
                            printf("Okay we are at todays count of %d.\n", *         (&todaysCount));                        
                            printf("Add more? yes or no\n");
                            fgets(yesno,10,stdin);
                            }
                            firstrun = true;
                            if (strcmp("yes\n", yesno) == 0){
                            stop = false;
                            }                        
                            if (strcmp("no\n", yesno) == 0){
                            stop = true;        
                            }    
                        } while(!stop) ;
                     break;
                    
                
                    /*Will build onto
                    case cd:
                    break;
                    case cm:
    
                    break;        
                        
                    case default:
                    printf("I am sorry I did not understand");
                    break;*/
                    }
        
            }
        int returncals(char food[100])
                {
                    int cal;
                    if (strcmp("manual\n", food) == 0)
                        {
                            printf("How many calories are in the food?\n");
                            scanf("%d", &cal);
                        }
                        else if (strcmp("banana\n", food) == 0)
                        {
                            cal = 105;
                        }    
                        else 
                        {
                            cal = 0;
                        }
        
    
                return cal;
                }
    Here is the problem code.
    Code:
     scanf("%d", &cal);
    Once I run scanf it completly skips the following fgets.
    Code:
     fgets(yesno,10,stdin);
    The scanf gets a calorie value and goes back main and asks if the user wants to start the loop over but the loop starts over anyway and it is skipped. here is a image.
    fgets after scanf, skips over fgets?-error-png
    Does anyone know why it does this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's because most scanf formats (including %d) leave the \n on the input stream.
    Unfortunately for you, this \n is exactly what the following fgets() needs to immediately return as if it did "nothing" at all.

    The ideal solution is to use fgets() for all input.
    So each of your scanf calls would be say

    char temp[100];
    // other code
    fgets(temp,sizeof(temp),stdin);
    sscanf(temp,"%d",&cal);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and scanf
    By kkk in forum C Programming
    Replies: 10
    Last Post: 07-29-2011, 10:11 AM
  2. fgets prompt in while loop "skips"
    By Jesdisciple in forum C Programming
    Replies: 16
    Last Post: 04-12-2009, 06:55 PM
  3. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  4. what happens after 'fgets' and 'scanf'
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 03:04 PM
  5. Scanf->Fgets
    By 00Sven in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 02:39 PM