Could someone please tell me what is wrong with my program?

When I am running the program, it doesn't display the results I want. It seems like it is not running though the male_calculation and female_calculation functions. Thanks!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  #include <iostream>
#include <cctype>

using namespace std;

bool input(double current_weight, double height, double desired_weight, int age, int days, bool &gender);

void male_calculation (double current_weight, double height, double desired_weight, int age, int days);

void female_calculation (double current_weight, double height, double desired_weight, int age, int days);

void function_call ();


int main()
{

   int current_weight = 0, height = 0, desired_weight = 0, age = 0, days = 0;

   bool gender;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

  input (current_weight, height, desired_weight, age, days, gender);


   if (gender = true)
       male_calculation (current_weight, height, desired_weight, age, days);



               else
               female_calculation (current_weight, height, desired_weight, age, days);

   return 0;

}

bool input (double current_weight, double height, double desired_weight, int age, int days, bool& gender)

{

   cout << "This program will calculate the amount of excercises and the total calories\n"
           "That you need a day to acquire your desired weight.\n"
           "Enter your current weight\n";

           cin >> current_weight;

   cout << "Your height in inches?\n";

           cin >> height;

   cout << "Your desired weight?\n";

           cin >> desired_weight;

   cout << "Your age in year?\n";

           cin >> age;

   cout << "In how long do you want to reach your desired weight?\n"
           "Please enter the days.\n";

           cin >> days;

   cout << "Press M if you are male and F for female.\n";

           cin >> gender;

           gender = toupper(gender);

       if (gender = 'M')
           gender = true;



           else if (gender = 'F')
           gender = false;


}

void male_calculation (double current_weight, double height, double desired_weight, int age, int days)

{

   double weight_in_kilogram, height_in_cm, weight_difference, calories_to_lose, calories_to_gain_or_lose, men_bmr;

   double number_of_fast_food_meals, number_of_healthy_meals, calories_per_day, ounce_of_steak, total_calories;

   weight_in_kilogram = current_weight/2.2;

   height_in_cm = height*2.54;

   weight_difference = desired_weight - current_weight;

   men_bmr = 88.362 + (13.397*weight_in_kilogram) + (4.799*height_in_cm) - (5.677*age);

   calories_to_gain_or_lose = weight_difference*3500;

   calories_per_day = calories_to_gain_or_lose/days;

   number_of_fast_food_meals = calories_per_day/1655;

   number_of_healthy_meals = calories_per_day/665;

   ounce_of_steak = number_of_healthy_meals*8;

           if (weight_difference > 0)

   {

           cout << "In order to maintain your current weight, you will need " << men_bmr << " calories a day.\n";

           cout << "To be " << desired_weight << "lbs in " << days << " days, you will need to consume\n"
                   << calories_per_day << " calories a day on top of " << men_bmr << " calories needed to maintain your weight.\n"
                   "This is equivalent to " << number_of_fast_food_meals << " double double, " << number_of_fast_food_meals << " fries and " << number_of_fast_food_meals << " chocolate shake that you will need to eat a day from in and out.\n"
                   "However, if you are a healthy eater, you may choose to eat " << ounce_of_steak << " Oz of sirloin steak and \n"
                   << number_of_healthy_meals << " cup of broccoli and pasta a day.\n";


   }

   double minutes_to_run, minutes_to_walk, minutes_of_basketball;

   minutes_to_run = calories_per_day/(0.0385*17*current_weight);

   minutes_to_walk = calories_per_day/(0.0385*current_weight);

   minutes_of_basketball = calories_per_day/(0.0385*8*current_weight);

   cout << weight_difference;

           if (weight_difference < 0)

   {

              cout << "In order to lost " << weight_difference << "lbs in " << days <<" days, you will need one of the following activities if you consume " << men_bmr << " calories a day.\n"
                      << minutes_to_walk << " minutes of walking or\n"
                      << minutes_of_basketball << " minutes of playing basketball or\n"
                      << minutes_to_run << " minutes of running a day.\n";

   }

}

void female_calculation (double current_weight, double height, double desired_weight, int age, int days)

{

   double weight_in_kilogram, height_in_cm, weight_difference, calories_to_lose, calories_to_gain_or_lose, women_bmr;

   double number_of_fast_food_meals, number_of_healthy_meals, calories_per_day, ounce_of_steak, total_calories;

   weight_in_kilogram = current_weight/2.2;

   height_in_cm = height*2.54;

   weight_difference = desired_weight - current_weight;

   women_bmr = 447.593 + (9.247*weight_in_kilogram) + (3.098*height_in_cm) - (4.33*age);

   calories_to_gain_or_lose = weight_difference*3500;

   calories_per_day = calories_to_gain_or_lose/days;

   number_of_fast_food_meals = calories_per_day/1655;

   number_of_healthy_meals = calories_per_day/665;

   ounce_of_steak = number_of_healthy_meals*8;

           if (weight_difference > 0)

   {

           cout << "In order to maintain your current weight, you will need " << women_bmr << " calories a day.\n";

           cout << "To be " << desired_weight << "lbs in " << days << " days, you will need to consume\n"
                   << calories_per_day << " calories a day on top of " << women_bmr << " calories needed to maintain your weight.\n"
                   "This is equivalent to " << number_of_fast_food_meals << " double double, " << number_of_fast_food_meals << " fries and " << number_of_fast_food_meals << " chocolate shake that you will need to eat a day from in and out.\n"
                   "However, if you are a healthy eater, you may choose to eat " << ounce_of_steak << " Oz of sirloin steak and \n"
                   << number_of_healthy_meals << " cup of broccoli and pasta a day.\n";


   }

   double minutes_to_run, minutes_to_walk, minutes_of_basketball;

   minutes_to_run = calories_per_day/(0.0385*17*current_weight);

   minutes_to_walk = calories_per_day/(0.0385*current_weight);

   minutes_of_basketball = calories_per_day/(0.0385*8*current_weight);

           if (weight_difference < 0)

   {

              cout << "In order to lost " << weight_difference << "lbs in " << days <<" days, you will need one of the following activities if you consume " << women_bmr << " calories a day.\n"
                      << minutes_to_walk << " minutes of walking or\n"
                      << minutes_of_basketball << " minutes of playing basketball or\n"
                      << minutes_to_run << " minutes of running a day.\n";

   }

}
In main, line 29, you are doing an assignment, not a compare.
I'm sorry, I am confused with your response. I stated that if gender was true then the if loop should move on to the male calculation function. Isn't this what I am supposed to do?
= is for assignment
== is for comparison as kooth mentioned.

By the way c++ is a strong typed language you can't change the variable type like you are here:
1
2
3
4
5
6
           cin >> gender;

           gender = toupper(gender);

       if (gender = 'M')
           gender = true;


You also aren't returning the bool like you said you would be.
Last edited on
Okay, so I'm going to try to use the comparison method as you and kooth mentioned.

As for changing the variable, was I not suppose to type in gender in the parenthesis?

In my bool function, didn't I return the bool by setting gender equal to true or false?
Look closely you are reading in a character to the gender(it is a bool - true/false). You should use another variable for the m/f or have the user enter if they are male true or false (false would mean female). Then get rid of the if and the toupper and reassigning true/false.
I'm sorry, I am completely new to programming. Is this what you mean? I got rid of toupper for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Are you a male? Enter 'y' for yes and 'n' for no. \n";

           cin >> gender;

           //gender = toupper(gender);

       if (gender = 'y')
           gender = true;



      else   (gender = 'n')
           gender = false;
Last edited on
Again you can't have gender be a character and a boolean it is a strong typed language. Also you never fixed he comparison you are still assigning so it will always be male.

1
2
       if (gender = 'y')
           gender = true;

Notice how it is the same variable with two different value types. The only reason you aren't getting compiler errors is because anything but 0 is true so 'y' would be considered true.

What I meant was having them enter true/false or 0/1. To do the first version you have to enable boolalpha. for input it would be
1
2
3
4
std::boolalpha(std::cin);
//or
std::cin >> boolalpha;
//probably other ways too but these are easiest 
YOu can do the same with output by replace cin with cout. Basically it would be like this then:

1
2
cout << "Are you male?(true or false): ";
cin >> gender; //gender is now true or false 



1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

int main()
{
    bool sex;
    std::cout << "Are you male(true/false)? ";
    std::cin >> std::boolalpha >> sex;
    std::cout << std::boolalpha << "Male: " << sex << std::endl;
}
Are you male(true/false)? true
Male: true

http://coliru.stacked-crooked.com/a/00c14a43e31b5890
Okay, I see what you meant! I fixed that part, but when I run my program, I get my results to be 0.00. I don't know what could be wrong with the calculations since I'm not getting a decent number. Thanks for your help.
Now you get to learn how to debug.

Step through your code using your IDE's debugger, or if you don't have a debugger, add cout statements at key places in your calculations.
Topic archived. No new replies allowed.