Thread: Error message while running the code!

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    12

    Error message while running the code!

    How, do I call out wrong input If I insert numbers with alphabets/symbols. Here is an example of the program!

    Code:
    int main (void){
            int     grams_of_fat,   /* number of grams of fat in one serving */
                    total_calories; /* number of calories in one serving */
            float   fat_fraction,   /* fraction of calories due to fat */
                    percent;        /* percentage of total calories from fat */
    
            printf("This program will tell you how much of the calories in\n");
            printf("a food are from the food's fat content.\n\n");
    
            printf("How many grams of fat are in one serving?  ");
            scanf("%d",&grams_of_fat);
            printf("How many total calories are in one serving?  ");
            scanf("%d",&total_calories);
    
            fat_fraction = (grams_of_fat * CALORIES_PER_GRAM) / total_calories;
            percent = fat_fraction * 100;
    
            if (grams_of_fat == 1) {
                    printf("\nA food with 1 gram of fat ");
            } else {
                    printf("\nA food with %d grams of fat ",grams_of_fat);
            }
    
            if (total_calories == 1) {
                    printf("and 1 calorie per serving\n");
            } else {
                    printf("and %d calories per serving\n",total_calories);
            }
    
            printf("has %.2f%% of those calories from fat.\n\n",percent);
    
            return 0; }
    Last edited by asylumcyclop; 05-28-2018 at 04:23 PM.

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Scanf returns the number of items matched, so if it is less than what you expected (i.e., less than one for the format string "%d") then you have an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange error when running code
    By Truong111299 in forum C Programming
    Replies: 3
    Last Post: 05-22-2018, 09:54 PM
  2. Why do I get this error message when compiling this code?
    By Videogamer555 in forum C Programming
    Replies: 5
    Last Post: 09-28-2014, 03:35 AM
  3. Replies: 3
    Last Post: 05-11-2011, 02:53 PM
  4. Replies: 0
    Last Post: 11-05-2010, 07:30 PM
  5. error message code
    By Flex in forum C Programming
    Replies: 1
    Last Post: 02-27-2002, 12:21 PM

Tags for this Thread