I am writing a program as homework that gets input from the user for the calories in a food and the grams of fat, then calculates the percentage of calories from fat and says whether it's a lowfat food. It also has to validate that the inputted calories and fat are both greater than 0 and that the calories are greater than the fat grams multiplied by 9. Here's what I have, it runs and the input validation works but I keep getting 0 as an answer for the percentage and I can't figure out why. I ran it through a debugger separate from the program I used to compile (we have to use code::blocks but I don't like it so I used a different debugger) and it seemed to work fine there without any errors. So I'm confused why it just outputs zero when I run it in code::blocks.
I don't get 0 as the percentage when compiled in C::B or Visual Studio.
Please enter the calories of the food item
125
Please enter how many grams of fat are in the food item
1
The percentage of calories from fat is 0.072
This is considered a lowfat food since the calories from fat percentage is 0.072
Of course I don't know what your input data is. I made up some values.
Personally I'd tweak the code a bit, using doubles instead of floats. And declare the supposed global constants as explicitly const.
#include <iostream>
//constants
constint NINE { 9 };
constdouble LOW { 30.0 };
//function prototypes
void getInput(double& totalCalories, double& fatGrams); //get and validate user input
// void calcLow(double& percentFat); //calculate whether the food is lowfat <-- not defined yet
void lowMessage(double& percentFat); //output message if food is lowfat
int main()
{
//local variables
double totalCalories;
double fatGrams;
getInput(totalCalories, fatGrams);
double fatCalories { fatGrams * NINE };
double percentFat { (fatCalories / totalCalories) * 100 }; // yields percentage
std::cout << "\nThe percentage of calories from fat is " << percentFat << "%\n";
if (percentFat <= LOW)
lowMessage(percentFat);
} //end main
void getInput(double& calories, double& fatGrams)
{
while (true)
{
std::cout << "Please enter the calories of the food item: ";
std::cin >> calories;
std::cout << "Please enter how many grams of fat are in the food item: ";
std::cin >> fatGrams;
if (calories > 0 && fatGrams > 0 && (calories > fatGrams * NINE))
return;
std::cout << "Invalid entries.\n";
} //end while
}//end getInput
void lowMessage(double& percentFat)
{
std::cout << "This is considered a lowfat food since the calories from fat percentage is " << percentFat << "%\n";
} //end lowMessage
Please enter the calories of the food item: 125
Please enter how many grams of fat are in the food item: 1
The percentage of calories from fat is 7.2%
This is considered a lowfat food since the calories from fat percentage is 7.2%
Please enter the calories of the food item: 50
Please enter how many grams of fat are in the food item: 2
The percentage of calories from fat is 36%
Please enter the calories of the food item: 125
Please enter how many grams of fat are in the food item: 0
Invalid entries.
Please enter the calories of the food item: 125
Please enter how many grams of fat are in the food item: 1
The percentage of calories from fat is 7.2%
This is considered a lowfat food since the calories from fat percentage is 7.2%