My program keeps getting zero as an answer

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 #include <iostream>

using namespace std;

//constants
int NINE = 9;
double LOW = 0.3;

//function prototypes
void getInput (float& totalCalories, float& fatGrams); //get and validate user input
void calcLow (float& percentFat); //calculate whether the food is lowfat
void lowMessage (float& percentFat); //output message if food is lowfat

int main()
{
    //local variables
    float totalCalories;
    float fatGrams;
    float percentFat;
    float fatCalories;

    getInput (totalCalories, fatGrams);

    fatCalories = (fatGrams * NINE);
    percentFat = (fatCalories / totalCalories);
    cout << "The percentage of calories from fat is " << percentFat << endl;

    if (percentFat <= LOW)
        lowMessage (percentFat);


    return 0;
} //end main

void getInput (float& calories, float& fatGrams)
{
    cout << "Please enter the calories of the food item" << endl;
    cin >> calories;
    cout << "Please enter how many grams of fat are in the food item" << endl;
    cin >> fatGrams;

    while (calories <= 0 || fatGrams <= 0 || calories < fatGrams * NINE)
    {
        cout << "These are invalid numbers, please reenter the calories" << endl;
        cin >> calories;
        cout << "These are invalid numbers, please reenter the grams of fat" << endl;
        cin >> fatGrams;
    } //end while

}//end getInput

void lowMessage (float& percentFat)
{
    cout << "This is considered a lowfat food since the calories from fat percentage is " << percentFat << endl;
} //end lowMessage  
Last edited on
lexicution17 wrote:
I keep getting 0 as an answer for the percentage

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

//constants
const int    NINE { 9 };
const double 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%
Topic archived. No new replies allowed.