/* Fat Gram Calculator - This program asks for the number of calories and fat grams
in a food. The program displays the percentage of calories that come from fat. If
the calories from fat are less than 30% of the total calories of the food, it also
displays a message indicating that the food is low on fat.
One gram of fat has 9 calories:
* Calories from fat = fat grams * 9
The percentage of calories from fat can be calculated as:
* Calories from fat / total calories
Input Validation: The number of calories and fat grams are not less than 0. Also,
the number of calories from fat cannot be greater than the total number of calories.
If that happens, an error message indicating that either the calories or fat grams
were incorrectly entered. */
#include "Utility.h"
int main()
{
/* Hold total calories, fat grams, calories from fat, and percentage of calories from fat */
double totalCalories,
fatGrams,
caloriesFromFat = 0,
pctCaloriesFromFat = 0;
/* Ask the user to enter calories and fat grams */
cout << "Enter the number calories contained in the food you plan to eat: ";
cin >> totalCalories;
cout << "Enter the number of fat grams contained in your food: ";
cin >> fatGrams;
/* Calculations */
caloriesFromFat = (fatGrams * 9);
pctCaloriesFromFat = (caloriesFromFat / totalCalories) * 100;
/* Format outpout */
cout << fixed << showpoint << setprecision(2);
/* Validating input and displaying results */
if (totalCalories <= 0 || fatGrams <= 0)
{
cout << "Invalid amount of calories or fat grams entered!\n";
}
else if (caloriesFromFat > totalCalories)
{
cout << "The number of calories of fat cannot be greater than the\n"
<< "total calories!\n";
}
else
{
(pctCaloriesFromFat < 30) ?
cout << "\nThe calories from fat in your food are: " << caloriesFromFat
<< " grams\n"
<< "The percentage of calories from fat in your food is: "
<< pctCaloriesFromFat << "%\n"
<< "Very good! Your food is very low on fat!\n" :
cout << "\nThe calories from fat in your food are: " << caloriesFromFat
<< " grams\n"
<< "The percentage of calories from fat in your food is: "
<< pctCaloriesFromFat << "%\n";
}
pauseSystem();
return 0;
}
Thursday, November 24, 2016
Programming Challenge 4.18 - Fat Gram Calculator
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment