User-Defined Functions and Call-by-Value help

Hi guys, I'm new to programming and I'm a bit stuck on a certain program. I've found someone else on this forum who literally copy and pasted our assignment but the solutions given to them don't really make any sense to me and I just need a little more guidance.

Anyways, our assignment is to make a program that calculates Basal Metabolic Rate (BMR) for men and women, and then, using that, outputs the number of bagels they can eat in a single day. It's required that all the cout and cin stuff ("Enter gender, etc) is within the main() function, while there are two other functions that perform the BMR calculation and subsequently pass it back to main() to calculate and output the number of bagels.

I've gotten the program to compile finally after an entire day of random/confusing errors that I couldn't understand, but now I'm stuck as to how I get the program to output the BMR and subsequently pass it back to main() to do the bagel calculations. It seems I understand all the little parts but when put together, I'm just confused. ):

Any help is extremely appreciated!!!! I'm brand new to this and I'm just really frustrated and upset with myself right now.

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>

using std::cout; using std::cin; using std::endl;

double bmrWomen(int weight, int heightInches, int age);
double bmrMen(int weight, int heightInches, int age);

int main() {
	
	char gender;
	cout << "Please enter your gender [F/M]: ";
	cin >> gender;

	int weight;
	cout << "Enter your weight in pounds: ";
	cin >> weight;

	int feet;
	int inches;
	cout << "Enter your height in feet and inches: ";
	cin >> feet >> inches;

	int heightInches;
	heightInches = (feet * 12) + inches;

	if (gender = 'F') {
		double bmrWomen(int weight, int heightInches, int age);
	}
	else {
		double bmrMen(int weight, int heightInches, int age);
	}

}

double bmrWomen(int weight, int heightInches, int age) {
	double bmr;
	bmr = 655 + (4.3 * weight) + (4.7 * heightInches) - (4.7 * age);
	cout << "Your BMR is: " << bmr << endl;
	return bmr;
}

double bmrMen(int weight, int heightInches, int age) {
	double bmr;
	bmr = 66 + (6.3 * weight) + (12.9 * heightInches) - (6.8 * age);
	cout << "Your BMR is: " << bmr << endl;
	return bmr;
}
First look your if statement needs double =. So ==.
should look like
1
2
3
4
5
6
if (gender == 'F') {
		double bmrWomen(int weight, int heightInches, int age);
	}
	else {
		double bmrMen(int weight, int heightInches, int age);
	}


Ok found more this code worked for me. Changes are commented.

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
#include <iostream>

using std::cout; using std::cin; using std::endl;

double bmrWomen(int , int , int); //the prototype only needs data type not variable
double bmrMen(int, int , int);

int main() {


	char gender;
	cout << "Please enter your gender [F/M]: ";
	cin >> gender;

	int age;
	cout << "Enter your age: ";// added age
	cin >> age;

	int weight;
	cout << "Enter your weight in pounds: ";
	cin >> weight;

	int feet;
	int inches;
	cout << "Enter your height in feet and inches: ";
	cin >> feet >> inches;

	int heightInches;
	heightInches = (feet * 12) + inches;

	if (gender == 'F') {
		bmrWomen(weight, heightInches, age);// removed ints and double
	}
	else {
		bmrMen(weight,  heightInches, age);// removed ints and double
	}

}

double bmrWomen(int weight, int heightInches, int age) {
	double bmr;
	bmr = 655 + (4.3 * weight) + (4.7 * heightInches) - (4.7 * age);
	cout << "Your BMR is: " << bmr << endl;
	return bmr;
}

double bmrMen(int weight, int heightInches, int age) {
	double bmr;
	bmr = 66 + (6.3 * weight) + (12.9 * heightInches) - (6.8 * age);
	cout << "Your BMR is: " << bmr << endl;
	return bmr;
}


Hope this helps!
Last edited on
That worked!!!! I can't believe I forgot to put in the age....Oops! You just saved my life by picking up on that, haha.

Now my next question is how do I pass the computed BMR back into main() to do a calculation with it?

I've tried doing something like this:

1
2
3
4
const int calories = 245;
int bagels;
bagels = bmr / calories;
cout << "number of bagels: " << bagels << endl;

But I'm not sure if that's how to properly use a named constant (it's a requirement we use one for this step), and I get an error saying (obviously) bmr is undefined. Thank you so much for all your help!!!!
Glad it helped. To fix this declare bmr above if statement and then set it equal to your function.

1
2
3
4
5
6
7
8
9
10
11
12
double bmr;
	if (gender == 'F') {
		bmr = bmrWomen(weight, heightInches, age);
	}
	else {
		bmr = bmrMen(weight, heightInches, age);
	}

	const int calories = 245;
	int bagels;
	bagels = bmr / calories;
	cout << "number of bagels: " << bagels << endl;
You are awesome!!! Thank you!
Topic archived. No new replies allowed.