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; }