Whats wrong with my code? Time running out.

My goal is to use the functions that will input a person's weight, an estimate for the intensity of physical activity, the number of minutes spent performing the physical activity, and the number of calories in one serving of your favorite food. Then the program should calculate and output how many servings of that food should be eaten per day to maintain the person's current weight at the specified activity level.

my problem is, in these lines: all of the calculations aren't being calculated correctly. I'm not sure what the problem is and I've been at it for hours.

cout << "Given the information you entered,\n";
cout << "You should be consuming the following caloric amounts:\n";
cout << bmr << " calories to maintain your body's Basal Metabolic Rate.\n";
cout << cals_activity << " calories to perform the physical activty for " << timeActivity << " minutes.\n";
cout << "The energy required for digestion = " << e_digest << " calories.\n";
cout << "The total amount of servings you should consume of your\n";
cout << "favorite food per day to maintain your current weight\n";
cout << "at the specified activity level is: " << servings << " servings.\n";

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
  #include <iostream>
#include <cmath>
using namespace std;

void input(double&weight, double&timeActivity, double&intensity, double&calsFavorite);
double getBmr(double bmr);
double getCals_activity(double cals_activity);
double getE_digest(double e_digest);
double getServings(double servings);
void output(double&weight, double&timeActivity, double&intensity, double&calsFavorite);

int main() {

	double weight, timeActivity, intensity, calsFavorite;
	double bmr, cals_activity, e_digest, servings;

//	bmr = 0;
//	cals_activity = 0;
//	e_digest = 0;
//	servings = 0;

	input(weight, timeActivity, intensity, calsFavorite);
	getBmr(bmr);
	getCals_activity(cals_activity);
	getE_digest(e_digest);
	getServings(servings);
	output(weight, timeActivity, intensity, calsFavorite);

	cout << "Given the information you entered,\n";
	cout << "You should be consuming the following caloric amounts:\n";
	cout << bmr << " calories to maintain your body's Basal Metabolic Rate.\n";
	cout << cals_activity << " calories to perform the physical activty for " << timeActivity << " minutes.\n";
	cout << "The energy required for digestion = " << e_digest << " calories.\n";
	cout << "The total amount of servings you should consume of your\n";
	cout << "favorite food per day to maintain your current weight\n";
	cout << "at the specified activity level is: " << servings << " servings.\n";

}

void input(double&weight, double&timeActivity, double&intensity, double&calsFavorite) {

	cout << "Enter your weight in pounds.\n";
	cin >> weight;

	cout << "Enter the amount of time spent doing physical activity.\n";
	cin >> timeActivity;

	cout << "Estimate the intensity level of the physical activity.\n";
	cin >> intensity;

	cout << "Enter the amount of calories in one serving of your favorite food.\n";
	cin >> calsFavorite;

}

double getBmr(double bmr) {

	double weight;
	bmr = 70 * pow((weight / 2.2) ,0.756);

return bmr;
}

double getCals_activity(double cals_activity) {

	double intensity, weight, minutes;
	cals_activity = 0.0385 * intensity * weight * minutes;

return cals_activity;
}

double getE_digest(double e_digest) {

	double bmr, cals_activity;
	e_digest = (bmr + cals_activity) * 0.1;

return e_digest;
}

double getServings(double servings) {

	double bmr, cals_activity, calsFavorite;
	servings = (bmr + cals_activity) / calsFavorite;

return servings;
}

void output(double&weight, double&timeActivity, double&intensity, double&calsFavorite) {


//	double bmr, cals_activity, e_digest, servings;
//	double weight, timeActivity, intensity, calsFavorite;

	cout << "Information entered: \n";
	cout << "Weight: " << weight << " lbs.\n";
	cout << "Time spent doing activity: " << timeActivity << " minutes.\n";
	cout << "Intensity of activity = " << intensity << ".\n";
	cout << "Calories in one serving\n";
	cout << "of your favorite food = " << calsFavorite << " calories.\n\n";
}
Last edited on
Perhaps you need to increase your compiler warning levels then listen to your compiler?

In function 'double getBmr(double)': 59:38: warning: 'weight' is used uninitialized in this function [-Wuninitialized] In function 'double getCals_activity(double)': 67:25: warning: 'intensity' is used uninitialized in this function [-Wuninitialized] 67:37: warning: 'weight' is used uninitialized in this function [-Wuninitialized] 67:55: warning: 'minutes' is used uninitialized in this function [-Wuninitialized] In function 'double getE_digest(double)': 75:18: warning: 'bmr' is used uninitialized in this function [-Wuninitialized] 75:18: warning: 'cals_activity' is used uninitialized in this function [-Wuninitialized] In function 'double getServings(double)': 83:18: warning: 'bmr' is used uninitialized in this function [-Wuninitialized] 83:18: warning: 'cals_activity' is used uninitialized in this function [-Wuninitialized] 83:49: warning: 'calsFavorite' is used uninitialized in this function [-Wuninitialized]
@jlb So if I'm correct what the compiler is saying is that those variables don't have any value to them since they're "uninitialized"? also, how do I go about increasing my compiler warning levels?

thanks
Last edited on
closed account (E0p9LyTq)
fiddlesticks12 wrote:
how do I go about increasing my compiler warning levels?

What compiler/IDE do you use? Hard to tell you how to change your warning levels without that information.
@FurryGuy I'm not entirely sure to be honest, I'm on my MacBook if that helps. But I don't quite know what an IDE is as well sorry.
integrated development environment. An IDE has a text editor, compiler, linker, project management, etc all in one.

How do you compile your program? Different tools change the warning differently.
@jonnin when I compile my program, enter "g++ 'filename'" if thats what you mean
enter: g++ -std=c++14 -Wall -Wextra -pedantic-errors filename

-Wall - generate most of the common warnings
-Wextra - and a few more helpful ones
-std=c++14 - know that this is C++ code (tell it once)
-pedantic-errors - I really did mean standard C++ (tell it once more, otherwise it doesn't listen)
now that see the variables in those functions need to be initialized, how might I start in doing so? I just can't quite figure it out no matter which way I think about it and try. not asking for the solution to my problem but just another step in the right direction
Here are two of the functions modified so that they get the information they need.
Do likewise for the remaining functions.

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
#include <iostream>
#include <cmath>
// using namespace std;

void input( double&weight, double&timeActivity, double&intensity, double&calsFavorite );

// pass weight as input, get bmr as the result
double getBmr( double weight );

// pass intensity, weight, and time in minutes as input
// get cals_activity as the result
double getCals_activity( double intensity, double weight, double minutes );

// declaration of other functions

int main() {

	double weight, timeActivity, intensity, calsFavorite;
	input(weight, timeActivity, intensity, calsFavorite);

	const double bmr = getBmr(weight);
	const double cals_activity = getCals_activity( intensity, weight, timeActivity );

	std::cout << bmr << '\n' << cals_activity << '\n' ; // *** testing, to be commeted out

	// rest of the code
}

void input( double&weight, double&timeActivity, double&intensity, double&calsFavorite ) {

    std::cout << "Enter your weight in pounds.\n";
    std::cin >> weight;

    std::cout << "Enter the amount of time spent doing physical activity.\n";
    std::cin >> timeActivity;

    std::cout << "Estimate the intensity level of the physical activity.\n";
    std::cin >> intensity;

    std::cout << "Enter the amount of calories in one serving of your favorite food.\n";
    std::cin >> calsFavorite;
}

//double getBmr(double bmr) {
double getBmr( double weight ) {

	//double weight;
	/*bmr =*/ return 70 * pow((weight / 2.2) ,0.756);

    // return bmr;
}

// double getCals_activity(double cals_activity) {
double getCals_activity( double intensity, double weight, double minutes ) {

	// double intensity, weight, minutes;
	/*cals_activity = */ return 0.0385 * intensity * weight * minutes;

    // return cals_activity;
}

// definitions of other functions 
Topic archived. No new replies allowed.