BMR Calculations

I'm having problems with this BMR program. It isn't running as well as I thought it would. Can someone help me? This is what I have so far

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
//declare a variable called gender that can hold bool
	bool gender;
	//declare a variable called age, weight, and height that can hold large decimals
	float weight, height;
	//declare a variable called num that can hold whole numbers
	int num, age;
	//declare a variable called bmr
	double bmr;
	//prompt the user to "Enter 10 if male or enter 9 if female:" and a endline
	cout << "Enter 10 if male or enter 9 if female:" << endl;
	//wait for input and store it in num
	cin >> num;
	//check to see if num is equal to 10
	if (num == 10){
		//user is a male
		gender = true;
	}else if (num == 9){
		//user is female
		gender = false;
	}
	//prompt the user to "Enter your age:" and a endline
	cout << "Enter your age:" << endl;
	//wait for input and store in age
	cin >> age;
	//prompt the user to "Enter your weight:" and a endline
	cout << "Enter your weight:" << endl;
	//wait for input and store in weight
	cin >> weight;
	//prompt the user to "Enter your height:" and a endline
	cout << "Enter your height:" << endl;
	//wait for input and store in height
	cin >> height;
	//calculate the bmr
	if (gender == 10){
		bmr = 66.5 + (13.8*weight) + (5.0*height) - (6.8*age);
	}
	if (gender == 9){
		bmr = 655.1 + (9.6*weight) + (1.8*height) - (4.7*age);
	}
	//print "Your BMR is:" and print the user bmr on screen
	cout << "Your BMR is:" << bmr << endl;
What exactly is the problem you are having?
When I try to run it, the program says that bmr is not initialized
Yeah you should make sure when you declare any variables to initialize them.

Instead of double bmr; , you should initialize the value double bmr = 0; Not initializing values for variables upon declaration can have very bad consequences. Unless you're using large arrays and performance is an issue, it's probably a good idea to do always initialize your variables.
Okay I tried it and now it says that the BMR is 0.
oh sorry I missed this - It's because you have a logic error. gender is a bool, but you have if(gender == 10) and if(gender == 9).

Of course you should be checking if it is true or false. ;)
I put that in the beginning. I have to put it up again when I'm calculating it? so it will be if(gender == 9){
gender = false;
bmr = 655.1 + (9.6*weight) + (1.8*height) - (4.7*age);

is that right?
No. Here's the problem:

1
2
3
4
5
6
7
8
int a;
int x = 12;

if (x = 84) a = 9;

if (x = 27) a = -7;

cout << a; // compiler complains that a may not be initialized (because it isn't) 

By the way, a better name than 'gender' would be something like 'is_male'.

Then, at the bottom, you can:

1
2
3
4
5
6
7
8
  // calculate BMR
  if (is_male) {
    ...
  } else {
    ...
  }

  cout << "Your BMR is: " << bmr << ".\n";

Hope this helps.
Last edited on
No.

Gender is a bool. A bool can hold only two values, true OR false (1 OR 0). It cannot and will not ever be 9 or 10. You must check for if(gender == true) and if(gender == false) appropriately, according to your code here:

1
2
3
4
5
6
7
//check to see if num is equal to 10
	if (num == 10){
		//user is a male
		gender = true;
	}else if (num == 9){
		//user is female
		gender = false;


Edit: Typo
Last edited on
So I did that part right yes? Or are you saying I did it wrong?
No, what you did is not correct. Please read my posts carefully.
No.
Gender is a bool. A bool can hold only two values, true OR false (1 OR 0). It cannot and will not ever be 9 or 10.
You must check for if(gender == true) and if(gender == false)
For more clarification this bit is wrong:

1
2
3
4
5
6
	-->if (gender == 10){
		bmr = 66.5 + (13.8*weight) + (5.0*height) - (6.8*age);
	}
	-->if (gender == 9){
		bmr = 655.1 + (9.6*weight) + (1.8*height) - (4.7*age);
	}
Thank you so much you guys. I got it to work with your help
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
//declare a variable called gender that can hold bool
	bool gender;
	//declare a variable called age, weight, and height that can hold large decimals
	float weight, height;
	//declare a variable called num that can hold whole numbers
	int num, age;
	//declare a variable called bmr
	double bmr = 0;
	//prompt the user to "Enter 10 if male or enter 9 if female:" and a endline
	cout << "Enter 1 if male or enter 0 if female:" << endl;
	//wait for input and store it in num
	cin >> num;
	//check to see if num is equal to 10
	if (num == 1){
		//user is a male
		gender = true;
	}else if (num == 0){
		//user is female
		gender = false;
	}
	//prompt the user to "Enter your age:" and a endline
	cout << "Enter your age:" << endl;
	//wait for input and store in age
	cin >> age;
	//prompt the user to "Enter your weight:" and a endline
	cout << "Enter your weight:" << endl;
	//wait for input and store in weight
	cin >> weight;
	//prompt the user to "Enter your height:" and a endline
	cout << "Enter your height:" << endl;
	//wait for input and store in height
	cin >> height;
	//calculate the bmr
	if (gender == 1){
		bmr = 66.5 + (13.8*weight) + (5.0*height) - (6.8*age);
	}
	if (gender == 0){
		bmr = 655.1 + (9.6*weight) + (1.8*height) - (4.7*age);
	}
Topic archived. No new replies allowed.