# Help with Functions!

Page 1 of 1

## 3 Replies - 1392 Views - Last Post: 16 February 2009 - 02:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86904&amp;s=a30afc8817114abd1d2e2203427a70db&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yltennis2822

Reputation: 0
• Posts: 4
• Joined: 15-February 09

# Help with Functions!

Posted 15 February 2009 - 10:20 PM

I'm a beginner and I'm sure this is pretty simple... but I'm having trouble with functions. Below is my code and after the gender input I get stuck... Any help would be greatly appreciated.

```#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

void Calculate (double, double, int);
double Calc_Ideal (double, int);
double Calc_Lean (double, double, int);
double Calc_BMI (double, double);
int main(){
int choice, gender;
double height, weight;
cout << "BODY COMPOSITION PROGRAM" << endl << endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;

if(choice==2){
cout<<endl<<"ERROR: Insufficient Data!"<<endl<<endl;
cout << "BODY COMPOSITION PROGRAM" << endl<<endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;
}
if(choice==1){
cout<<endl<<"Enter your height in inches and weight in pounds: ";
cin>>height>>weight;
cout<<endl<<"Enter your gender (1 = Female & 2 = Male): ";
cin>>gender;

if(gender==1 || gender==2){

void Calculate (double, double, int);
}
else{
cout<<"ERROR: Invalid Gender!"<<endl<<endl;
return 0;
}}
}
void Calculate (double height, double weight, int gender){
double ideal, idealpounds, lean, leanpounds, BMI;

double Calc_Ideal (double, int);
cout <<"Your ideal body weight is "<<ideal<<" kg = " <<idealpounds<< " lb."<<endl;
double Calc_Lean (double, double, int);
cout <<"Your lean body weight is "<<lean<<" kg = "<<leanpounds<<" lb."<<endl;
double Calc_BMI (double, double);
cout <<"Your Body Mass Index was determined to be "<<BMI<<".";
if(BMI<25){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as normal.";
}
if(BMI<30){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as overweight.";
}
else{
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as obese.";
}}

double Calc_Ideal (double height, int gender){
double ideal, idealpounds;
if(gender=1){
ideal= 45.5 + 2.3 * (height - 60);
idealpounds = ideal/2.2026;
}
if(gender=2){
ideal = 50 + 2.3 * (height - 60);
idealpounds = ideal/2.2026;
}
return ideal, idealpounds;
}

double Calc_Lean (double height, double weight, int gender){
double lean, leanpounds;
if(gender=1){
lean = (1.07 * (weight)) - (148 * (pow(weight,2) /(100 * pow(height,2))));
leanpounds=lean/2.2026;
}
if(gender=2){
lean = ((1.10 * (weight)) - (128 * (pow(weight,2) /(100 * pow(height,2)))));
leanpounds=lean/2.2026;
}
return lean;
}

double Calc_BMI (double height, double weight){
double BMI;
BMI = weight/pow(height,2);
return BMI;
}

```

*edit: Please use code tags in the future, thanks!

This post has been edited by Martyr2: 15 February 2009 - 10:37 PM

Is This A Good Question/Topic? 0

## Replies To: Help with Functions!

### #2 FBIRyan

Reputation: 0
• Posts: 4
• Joined: 15-February 09

## Re: Help with Functions!

Posted 15 February 2009 - 11:33 PM

yltennis2822, on 15 Feb, 2009 - 09:20 PM, said:

I'm a beginner and I'm sure this is pretty simple... but I'm having trouble with functions. Below is my code and after the gender input I get stuck... Any help would be greatly appreciated.

```#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

void Calculate (double, double, int);
double Calc_Ideal (double, int);
double Calc_Lean (double, double, int);
double Calc_BMI (double, double);
int main(){
int choice, gender;
double height, weight;
cout << "BODY COMPOSITION PROGRAM" << endl << endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;

if(choice==2){
cout<<endl<<"ERROR: Insufficient Data!"<<endl<<endl;
cout << "BODY COMPOSITION PROGRAM" << endl<<endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;
}
if(choice==1){
cout<<endl<<"Enter your height in inches and weight in pounds: ";
cin>>height>>weight;
cout<<endl<<"Enter your gender (1 = Female & 2 = Male): ";
cin>>gender;

if(gender==1 || gender==2){

void Calculate (double, double, int);
}
else{
cout<<"ERROR: Invalid Gender!"<<endl<<endl;
return 0;
}}
}
void Calculate (double height, double weight, int gender){
double ideal, idealpounds, lean, leanpounds, BMI;

double Calc_Ideal (double, int);
cout <<"Your ideal body weight is "<<ideal<<" kg = " <<idealpounds<< " lb."<<endl;
double Calc_Lean (double, double, int);
cout <<"Your lean body weight is "<<lean<<" kg = "<<leanpounds<<" lb."<<endl;
double Calc_BMI (double, double);
cout <<"Your Body Mass Index was determined to be "<<BMI<<".";
if(BMI<25){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as normal.";
}
if(BMI<30){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as overweight.";
}
else{
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as obese.";
}}

double Calc_Ideal (double height, int gender){
double ideal, idealpounds;
if(gender=1){
ideal= 45.5 + 2.3 * (height - 60);
idealpounds = ideal/2.2026;
}
if(gender=2){
ideal = 50 + 2.3 * (height - 60);
idealpounds = ideal/2.2026;
}
return ideal, idealpounds;
}

double Calc_Lean (double height, double weight, int gender){
double lean, leanpounds;
if(gender=1){
lean = (1.07 * (weight)) - (148 * (pow(weight,2) /(100 * pow(height,2))));
leanpounds=lean/2.2026;
}
if(gender=2){
lean = ((1.10 * (weight)) - (128 * (pow(weight,2) /(100 * pow(height,2)))));
leanpounds=lean/2.2026;
}
return lean;
}

double Calc_BMI (double height, double weight){
double BMI;
BMI = weight/pow(height,2);
return BMI;
}

```

*edit: Please use code tags in the future, thanks!

To fix one crashing problem, change the following:

This:
```if(gender==1 || gender==2){

void Calculate (double, double, int);
```

To:
```if(gender==1 || gender==2){

Calculate (height, weight, gender);
```

As you can see when you call a function you would type it's name then the parenthesis.(With the data you want to send.)
```Calculate (height, weight, gender);
```

This won't fix your other problem though. In Calc_Ideal(); you try to return two things. You can only return one thing. So you would have to make two functions, one for ideal and one for idealpounds. You'd call them like this within your Calculate function:
```ideal = Calc_Ideal (height, gender);
```

```idealpounds = Calc_IdealPounds (ideal, gender);
```

You'd have to make Calc_IdealPounds first. The reason why I don't call height into in Calc_IdealPounds, is because I won't be needing it. ;P On the other hand, we will need ideal.
You would have to do the same for Calc_Lean. Make two functions, one to return lean, and one to return leanpounds.
You'd have to also call your other functions the same way as I told you for Calc_Ideal.
Also in the following code:
```		 if(BMI<25){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as normal.";
}
if(BMI<30){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as overweight.";
}
else{
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as obese.";
}
```

You should change it to this:
```		if(BMI<25){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as normal.";
}
else if(BMI<30){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as overweight.";
}
else{
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as obese.";
}
system("PAUSE");
```

Without the else if, it will consider both if's as two if's. If your BMI was 20 it would say both the top two. The system("PAUSE"); is so it waits for user input before closing, giving the user the chance to read what your program has to say about him.
Here's your code with all the fixes:
```#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

void Calculate (double, double, int);
double Calc_Ideal (double, int);
double Calc_IdealPounds (double, int);
double Calc_Lean (double, double, int);
double Calc_LeanPounds (double, int);
double Calc_BMI (double, double);
int main(){
int choice, gender;
double height, weight;
cout << "BODY COMPOSITION PROGRAM" << endl << endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;

if(choice==2){
cout<<endl<<"ERROR: Insufficient Data!"<<endl<<endl;
cout << "BODY COMPOSITION PROGRAM" << endl<<endl;
cout << "Menu Choices: "<< endl<<endl;
cout << "  0 - Quit" << endl <<"  1 - Enter your height (inches) & weight (lbs.) & gender (1=female & 2=male)"<<endl<< "  2 - Calculate IBW, LBW, and BMI"<<endl<<endl;
cin >> choice;
}
if(choice==1){
cout<<endl<<"Enter your height in inches and weight in pounds: ";
cin>>height>>weight;
cout<<endl<<"Enter your gender (1 = Female & 2 = Male): ";
cin>>gender;

if(gender==1 || gender==2){

Calculate(height, weight, gender);
}
else{
cout<<"ERROR: Invalid Gender!"<<endl<<endl;
return 0;
}}
}
void Calculate (double height, double weight, int gender){
double ideal, idealpounds, lean, leanpounds, BMI;

ideal = Calc_Ideal (height, gender);
idealpounds = Calc_IdealPounds (ideal, gender);
cout <<"Your ideal body weight is "<<ideal<<" kg = " <<idealpounds<< " lb."<<endl;
lean = Calc_Lean (height, weight, gender);
leanpounds = Calc_LeanPounds (lean, gender);
cout <<"Your lean body weight is "<<lean<<" kg = "<<leanpounds<<" lb."<<endl;
BMI = Calc_BMI (height, weight);
cout <<"Your Body Mass Index was determined to be "<<BMI<<".";
if(BMI<25){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as normal."<<endl;
}
else if(BMI<30){
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as overweight."<<endl;
}
else{
cout <<"According to the National Heart, Lung, and Blood Institute you would be classified as obese."<<endl;
}
system("PAUSE");
}

double Calc_Ideal (double height, int gender){
double ideal;
if(gender=1){
ideal= 45.5 + 2.3 * (height - 60);
}
if(gender=2){
ideal = 50 + 2.3 * (height - 60);
}
return ideal;
}

double Calc_IdealPounds (double ideal, int gender){
double idealpounds;
if(gender=1){
idealpounds = ideal/2.2026;
}
if(gender=2){
idealpounds = ideal/2.2026;
}
return idealpounds;
}

double Calc_Lean (double height, double weight, int gender){
double lean, leanpounds;
if(gender=1){
lean = (1.07 * (weight)) - (148 * (pow(weight,2) /(100 * pow(height,2))));
}
if(gender=2){
lean = ((1.10 * (weight)) - (128 * (pow(weight,2) /(100 * pow(height,2)))));
}
return lean;
}

double Calc_LeanPounds (double lean, int gender){
double leanpounds;
if(gender=1){
leanpounds=lean/2.2026;
}
if(gender=2){
leanpounds=lean/2.2026;
}
return leanpounds;
}

double Calc_BMI (double height, double weight){
double BMI;
BMI = weight/pow(height,2);
return BMI;
}
```

Regards,
Ryan

This post has been edited by FBIRyan: 15 February 2009 - 11:39 PM

### #3 yltennis2822

Reputation: 0
• Posts: 4
• Joined: 15-February 09

## Re: Help with Functions!

Posted 15 February 2009 - 11:54 PM

Thank you!

### #4 FBIRyan

Reputation: 0
• Posts: 4
• Joined: 15-February 09

## Re: Help with Functions!

Posted 16 February 2009 - 02:32 AM

yltennis2822, on 15 Feb, 2009 - 10:54 PM, said:

Thank you!

Sure thing!