Pages: 12
I'm attempting to compute the adjusted BMR using an overloaded function. The first function correctly computed the BMR, but when I get to the overloaded function (adds in activity level) I'm running into problems. I have a switch for my activity level, and I can't seem to get it to work with my overloaded function. I've run into so many problems with the adjusted BMR that my code for that section is a mess. Any help would be greatly appreciated. Thanks.

Here's what I have so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include using namespace std; #include double maxHeartRate(int x); double BMR(char sex, int weight, int inches, int x); double BMR(char sex, int weight, int inches, int x, int alev); int main() { int age; int weight; int inches; int activityLevel; char gender; cout << "Enter your age (in years), then press return: " << endl; cin >> age; cout << "Enter your gender by typing M for male or F for female: " << endl; cin >> gender; cout << "Enter your weight (in pounds), then press return: " << endl; cin >> weight; cout << "Enter your height (in inches), then press return: " << endl; cin >> inches; cout << "Please enter your activity level:" << endl; cout << "Press 1 for Sedentary" << endl; cout << "Press 2 for Somewhat Active (exercise occasionally)" << endl; cout << "Press 3 for Active (exercise 3-4 days per week)" << endl; cout << "Press 4 for Highly Active (exercise every day)" << endl; cin >> activityLevel; cout << "Your maximum heart rate is: " << maxHeartRate(age) << endl; cout << "Your BMR is: " << BMR(gender, weight, inches, age) << endl; system("pause"); return 0; } double maxHeartRate(int x) { return 205.8 - (0.685 * x); } double BMR(char sex, int weight, int inches, int x) { if (sex == 'F') { return 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x); } if (sex == 'M') { return 66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x); } } double BMR(char sex, int weight, int inches, int x, int alev) { switch (alev) { case 1: alev = BMR * 1.2; break; case 2: alev = bmr * 1.3; break; case 3: alev = bmr * 1.4; break; case 4: alev = bmr * 1.5; break; }``````
 ``123456789101112131415161718`` ``````//Don't pass parameters you're not going to use. double BMR(char sex, int weight, int inches, int x, int alev) { switch (alev) { case 1: //Note: BMR is not the same as bmr! return BMR * 1.2; case 2: return bmr * 1.3; case 3: return bmr * 1.4; case 4: return bmr * 1.5; } //Or simplified: //return bmr * (1.0 + (alev + 1) * 0.1); }``````
You can not duplicate identical functions "BMR". And it is simply to realize your program by one function "BMR":
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #include #include double maxHeartRate(int x); double BMR(char sex, int weight, int inches, int x, int alev, float chooseBMR); int main() { int age; int weight; int inches; int activityLevel; char gender; int alev; float chooseBMR; cout << "Enter your age (in years), then press return: " << endl; cin >> age; cout << "Enter your gender by typing M for male or F for female: " << endl; cin >> gender; cout << "Enter your weight (in pounds), then press return: " << endl; cin >> weight; cout << "Enter your height (in inches), then press return: " << endl; cin >> inches; cout << "Please enter your activity level:" << endl; cout << "Press 1 for Sedentary" << endl; cout << "Press 2 for Somewhat Active (exercise occasionally)" << endl; cout << "Press 3 for Active (exercise 3-4 days per week)" << endl; cout << "Press 4 for Highly Active (exercise every day)" << endl; cin >> activityLevel; cin.ignore(1000, '\n'); cout << "Your maximum heart rate is: " << maxHeartRate(age) << endl; cout << "Your BMR is: " << BMR(gender, weight, inches, age, alev, chooseBMR) << endl; getch(); return 0; } double maxHeartRate(int x) { return 205.8 - (0.685 * x); } double BMR(char sex, int weight, int inches, int x, int alev, float chooseBMR) { if (sex == 'F') { return chooseBMR = 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x); } if (sex == 'M') { return chooseBMR = 66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x); } switch (alev) { case 1: return chooseBMR * 1.2; case 2: return chooseBMR * 1.3; case 3: return chooseBMR * 1.4; case 4: return chooseBMR * 1.5; } return chooseBMR; } ``````
Last edited on
Quote:
 You can not duplicate identical functions "BMR"

http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
Thanks. Now it's more clear for me. It's an adjustment to my previous post here.
Thank you Chervil. I need to overload the function per the assignment, which is where I'm having difficulties. Greatly appreciate all of the feedback. Novice here who's stumped.
Last edited on
I've not gone deeply into this but so far, this looks like an invalid syntax:
`alev = BMR * 1.2;` since BMR isn't the name of a variable - it is a function which should be called.

valid syntax might look like this:
 ``1234`` ``````double BMR(char sex, int weight, int inches, int x, int alev) { return alev * BMR( sex, weight, inches, x); }``````

Note: I don't think the above is the actual correct code, you will need to write something which implements the required rules for calculating the result.

Thanks again, Chervil. I don't actually have to use a switch, so I might try the if/if else route. This would be much simpler without using an overloaded function, but oh well. Appreciate everyone's willingness to help this greenhorn.
An alternative to switch-case and if-else is to use a lookup table.

Since the user must input an integer from 1 to 4 for `activityLevel`,
you could use a table (array) to look up the required value.
for example:
 ``1234567`` `````` double adjust[4] = {1.2, 1.3, 1.4, 1.5}; double factor = adjust[1]; // default to "somewhat active" if (alev > 0 && alev < 5) // check user input is within range factor = adjust[alev-1]; // Remember array subscripts start from zero. ``````

Thanks again Chervil. So I'd just include that in the overloaded function?
 So I'd just include that in the overloaded function?

You could use that, or something similar, wherever you were thinking of using the switch/case or if/else.
Thanks to your help, I've moved past the overloaded function to calculate adjusted BMR and am now stumped on the final requirement: "A void function should be created for displaying the results. This function will take two parameters: a string with the name of the calculation (like "maximum heart rate") and the value of that calculation. The display should be something like: "Your maximum heart rate is 188."

All of my formulas and calculations run correctly, but this void menu has me stumped. Here's what I have right now, which isn't working

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141`` ``````#include using namespace std; #include double maxHeartRate(int x); double BMR(char sex, int weight, int inches, int x); double BMR(char sex, int weight, int inches, int x, int alev); double BMI(double lbs, double inch); void displayResults(string calculation, double z); int main() { int age; int weight; int inches; int activityLevel; char gender; string calculation; double result = 0; cout << "Enter your age (in years), then press return: " << endl; cin >> age; cout << "Enter your gender by typing M for male or F for female: " << endl; cin >> gender; cout << "Enter your weight (in pounds), then press return: " << endl; cin >> weight; cout << "Enter your height (in inches), then press return: " << endl; cin >> inches; cout << "Please enter your activity level:" << endl; cout << "Press 1 for Sedentary" << endl; cout << "Press 2 for Somewhat Active (exercise occasionally)" << endl; cout << "Press 3 for Active (exercise 3-4 days per week)" << endl; cout << "Press 4 for Highly Active (exercise every day)" << endl; cin >> activityLevel; cout << "Your maximum heart rate is: " << maxHeartRate(age) << endl; cout << "Your BMR is: " << BMR(gender, weight, inches, age) << endl; cout << "Your adjusted BMI is: " << BMR(gender, weight, inches, age, activityLevel) << endl; cout << "Your BMI is: " << BMI(weight, inches) << endl; cout << "Please enter which calculation you wish to perform --" << endl; cout << "To calculate your maximum heart rate enter MHR" << endl; cout << "To calculate your basal metabolic rate enter BMR" << endl; cout << "To calculate your adjusted basal metabolic rate enter ABMR" << endl; cout << "To calculate your body mass index enter BMI" << endl; displayResults(calculation, result); system("pause"); return 0; } double maxHeartRate(int x) { return 205.8 - (0.685 * x); } double BMR(char sex, int weight, int inches, int x) { if (sex == 'F') { return 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x); } if (sex == 'M') { return 66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x); } } double BMI(double lbs, double inch) //BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703 { return (lbs / (inch * inch)) * 703; } double BMR(char sex, int weight, int inches, int x, int alev) //Computes users adjusted BMR (BMR * activity level) { if (sex == 'F') { switch (alev) { case 1: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.2; break; case 2: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.3; break; case 3: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.4; break; case 4: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.5; } } if (sex == 'M') { switch (alev) { case 1: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.2; break; case 2: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.3; break; case 3: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.4; break; case 4: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.5; break; } } void displayResults(string calculation, double z); { if ("calculation" == "MHR") { cout << "Your maximum heart rate is: " << 'z'; } else if ("calculation" == "BMR") { cout << "Your BMR is: " << 'z'; } else if ("calculation" == "ABMR") { cout << "Your adjusted BMR is: " << 'z'; } else if ("calculation" == "BMI") { cout << "Your BMI is: " << 'z'; } } } ``````
Quote, "A void function should be created for displaying the results. This function will take two parameters: a string with the name of the calculation (like "maximum heart rate") and the value of that calculation. The display should be something like: "Your maximum heart rate is 188."

Currently your displayResults() function takes two parameters, but does not use either of them. It would probably take no more than one line of code to write the body of the function to satisfy the specifications above.

Also there is an unwanted semicolon at the end of line 121.
Last edited on
Thank you Chervil, but I'm still lost. And if I remove the semicolon on line 121 the compiler gives me the "error, expected a ':' " message.
In the most recent code posted above, there are two closing braces right at the end - why? The answer is that the second is the closing brace of function BMR which began on line 90.

If that closing brace is moved to its proper place - about line 120 - then the function displayResults() can be defined properly. Currently it is nested inside the previous function, which is why the compiler is complaining (you cannot define a named function inside another function).
I just caught that. Now back to this void function. I know this is elementary, but I'm in my 4th week of coding.
I think you need to pay strict attention to what the specification says. Of course after the function has been written, you will then need to write the code which actually calls the function. Such a function call might look like this, there will be several such calls:
 `` `` `` displayResults("maximum heart rate", maxHeartRate(age));``

That helped! Unfortunately, regardless of which function the user inputs, it now returns all four results. I greatly appreciate all of your help and patience.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161`` ``````double maxHeartRate(int x); double BMR(char sex, int weight, int inches, int x); double BMR(char sex, int weight, int inches, int x, int alev); double BMI(double lbs, double inch); void displayResult(string calculation, double result); int main() { int age; int weight; int inches; int activityLevel; char gender; string calculation; //Explanation of Program cout << "This program will give you the choice to compute one of the following: " << endl; cout << "-Maximum Heart Rate" << endl; cout << "-Basal Metabolic Rate" << endl; cout << "-Adjusted Basal Metabolic Rate" << endl; cout << "-Body Mass Index" << endl; cout << endl; cout << "Please enter your information, then press return" << endl; cout << endl; //Stores age variable cout << "Enter your age (in years), then press return: " << endl; cin >> age; //stores gender cout << "Enter your gender by typing M for male or F for female: " << endl; cin >> gender; //stores weight cout << "Enter your weight (in pounds), then press return: " << endl; cin >> weight; //stores height (inches) cout << "Enter your height (in inches), then press return: " << endl; cin >> inches; //stores activity level cout << "Please enter your activity level:" << endl; cout << "Press 1 for Sedentary" << endl; cout << "Press 2 for Somewhat Active (exercise occasionally)" << endl; cout << "Press 3 for Active (exercise 3-4 days per week)" << endl; cout << "Press 4 for Highly Active (exercise every day)" << endl; cin >> activityLevel; //user selections which calculation to perform cout << "Please enter which calculation you wish to perform --" << endl; cout << "To calculate your maximum heart rate enter MHR" << endl; cout << "To calculate your basal metabolic rate enter BMR" << endl; cout << "To calculate your adjusted basal metabolic rate enter ABMR" << endl; cout << "To calculate your body mass index enter BMI" << endl; cin >> calculation; displayResult("MHR", maxHeartRate(age)); cout << endl; displayResult("BMR", BMR(gender, weight, inches, age)); cout << endl; displayResult("ABMR", BMR(gender, weight, inches, age, activityLevel)); cout << endl; displayResult("BMI", BMI(weight, inches)); cout << endl; system("pause"); return 0; } double maxHeartRate(int x) { return 205.8 - (0.685 * x); } double BMR(char sex, int weight, int inches, int x) { if (sex == 'F') { return 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x); } else if (sex == 'M') { return 66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x); } else { cout << "You have entered an invalid character" << endl; } } double BMI(double lbs, double inch) //BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703 { return (lbs / (inch * inch)) * 703; } double BMR(char sex, int weight, int inches, int x, int alev) //Computes users adjusted BMR (BMR * activity level) { if (sex == 'F') { switch (alev) { case 1: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.2; break; case 2: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.3; break; case 3: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.4; break; case 4: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.5; } } if (sex == 'M') { switch (alev) { case 1: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.2; break; case 2: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.3; break; case 3: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.4; break; case 4: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.5; break; } } else { cout << "You have entered an invalid character" << endl; } } void displayResult(string calculation, double result) { if (calculation == "MHR") { cout << "Your maximum heart rate is: " << result; } else if (calculation == "BMR") { cout << "Your BMR is: " << result; } else if (calculation == "ABMR") { cout << "Your adjusted BMR is: " << result; } else if (calculation == "BMI") { cout << "Your BMI is: " << result; } } ``````
 Unfortunately, regardless of which function the user inputs, it now returns all four results.

In function main(), there is this prompt:
 `` `` ``cout << "Please enter which calculation you wish to perform --" << endl;``

Then there is a corresponding `cin >> calculation;`. however, I don't see where your program makes use of the value of `calculation`. You will need a series of if/else if statements.

As for function `displayResult()`, it seems to be doing much more than was called for. It is supposed to receive two parameters, a string and a number, and simply output those values, neatly formatted with the additional text , something like: "Your " parm1 " is " parm2. It would also make sense to output `endl` after the text, rather than putting that separately each time after calling the function.

Thank you. I changed things around so the code was contained in the switch. Everything seems to work other than the void function. I'm at the end of my rope with this one.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````double maxHeartRate(int x); double BMR(char sex, int weight, int inches, int x); double BMR(char sex, int weight, int inches, int x, int& alev); double BMI(double lbs, double inch); void displayResult(int selection, double result); int main() { int age; int weight; int inches; char gender; string calculation; int alev = 0; double result = 0; //Explanation of Program cout << "This program will give you the choice to compute one of the following: " << endl; cout << "-Maximum Heart Rate" << endl; cout << "-Basal Metabolic Rate" << endl; cout << "-Adjusted Basal Metabolic Rate" << endl; cout << "-Body Mass Index" << endl; cout << endl; cout << "Please enter your information, then press return" << endl; cout << endl; //Stores age variable cout << "Enter your age (in years), then press return: " << endl; cin >> age; //stores gender cout << "Enter your gender by typing M for male or F for female: " << endl; cin >> gender; //stores weight cout << "Enter your weight (in pounds), then press return: " << endl; cin >> weight; //stores height (inches) cout << "Enter your height (in inches), then press return: " << endl; cin >> inches; cout << BMR(gender, weight, inches, age); cout << BMR(gender, weight, inches, age, alev); cout << endl; system("pause"); return 0; } double maxHeartRate(int x) { return 205.8 - (0.685 * x); } double BMR(char sex, int weight, int inches, int x) { if (sex == 'F') { return 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x); } else if (sex == 'M') { return 66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x); } else { cout << "You have entered an invalid character" << endl; } } double BMR(char sex, int weight, int inches, int x, int& alev) //Computes users adjusted BMR (BMR * activity level) { cout << "Please enter your activity level:" << endl; cout << "Press 1 for Sedentary" << endl; cout << "Press 2 for Somewhat Active (exercise occasionally)" << endl; cout << "Press 3 for Active (exercise 3-4 days per week)" << endl; cout << "Press 4 for Highly Active (exercise every day)" << endl; cin >> alev; if (sex == 'F') { switch (alev) { case 1: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.2; break; case 2: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.3; break; case 3: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.4; break; case 4: return (655 + (4.35 * weight) + (4.7 * inches) - (4.7 * x)) * 1.5; } } else if (sex == 'M') { switch (alev) { case 1: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.2; break; case 2: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.3; break; case 3: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.4; break; case 4: return (66 + (6.23 * weight) + (12.7 * inches) - (6.8 * x)) * 1.5; break; } } else { cout << "You have entered an invalid character" << endl; } } double BMI(double lbs, double inch) //BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703 { return (lbs / (inch * inch)) * 703; } void displayResult(int selection, double result) { cout << "Please enter which calculation you wish to perform --" << endl; cout << "To calculate your maximum heart rate press 1" << endl; cout << "To calculate your basal metabolic rate press 2" << endl; cout << "To calculate your adjusted basal metabolic rate press 3" << endl; cout << "To calculate your body mass index press 4" << endl; cin >> selection; switch (selection) { case 1: cout << "Your maximum heart rate is: " << maxHeartRate; break; case 2: cout << "Your basal metabolic rate is: " << result; break; case 3: cout << "Your adjusted basal metabolic rate is: " << result; break; case 4: cout << "Your body mass index is: " << result; } ``````
Pages: 12