So I'm working on my final project, and I decided to create a BMI calculator.

As part of the requirements, I need to call a function into play. Below is what I have so far, but I am getting errors. I don't expect the solution, but could someone assist me by pointing me in the right direction.

The problem is that I am not calling the function into play correctly (for starters). How do I do that?

Thank you.
Sara

``````#include <iostream>

using namespace std;

int main()
{
int weight ; // weight in pounds
int height ; // height in inches
float BMI ; // Body Mass Index
bool DoItAgain ;
char Repeat ;

cout << " " << endl ;
cout << "Body Mass Index (BMI) Calculator" << endl ;
cout << " " << endl ;
cout << "Enter your weight in pounds. (please round to nearest whole number): " << endl ;

cin >> weight ;

cout << "Enter your height in inches. (please round to nearest whole number): " << endl ;

cin >> height ;

cout << "Your BMI is" BMI(weight, height) << endl ; This is where my first problem is.
if BMI < 18.5
cout << "You are underweight, and should considered gaining weight."

if BMI <= 24.9
cout << "Congratulations, you are a healthy weight."
"You should continue to maintain this weight" << endl ;

if BMI <= 29.9
cout << "You are over weight and should consider losing a little bit of weight." << endl ;

if BMI <= 39.9
cout << "You are obese and your weight could lead to serious health problems."

if BMI >= 40.0
cout << "You are severely obese and your weight may already be causing serious health complications."

cout << "Would you like to enter a new height and weight? (Y or N): " << endl ;
cin >> Repeat ;
if Repeat = N
DoItAgain = false

return 0 ;
}

int BMI(weight, height)
{
int wgt_kg // weight in kilograms
int hgt_m // height in meters

wgt_kg = (weight * 0.454) // kilograms = pounds * 0.454
hgt_m = (height * 0.0254) // meters = inches * 0.0254)
BMI = (wgt_kg/(hgt_m^2)) // BMI = kilograms/(meters^2)

return 0 ;
}
``````

Edited by Nick Evan: Fixed formatting

2
Contributors
9
Replies
11
Views
8 Years
Discussion Span
Last Post by daeuse

You have a few errors but I'll point you in the right direction.

1) As is you cannot use your function from main because it's defined after main but it wasn't declared before main ( so main doesn't see it). You can do one of two things to fix this:

a) Put your function at the top before main
b) Put a declaration at the top before main

``int BMI(int weight, int height);``

2) You are defining the function as int BMI(weight, height){} which is incorrect. What are "weight" and "height"? Are they int, double, string? You must place the correct type in there as I have shown under point 1.

3) It looks like you've got a couple of typos in there as well, but I'll let the compiler tell you where they are.

Edited by necrolin: n/a

Why are you using integers and multiplying them with numbers with a decimal point? You'll lose accuracy because everything after the decimal point will be cut from your integers. You'll get a more accurate calculation if you use doubles instead of integers throughout this work.

I did clean up many of the type-o's. I saw them after I posted my first message.

Thank you for your help. I'm getting closer, but now a new problem has popped up. I'm gettig a error C2447 saying somthing about "old style"

Here is what I have now:

``````#include <iostream>

using namespace std;

int BMI(int weight, int height);

int main()
{
int weight ; // weight in pounds
int height ; // height in inches
float BMI ; // Body Mass Index
bool DoItAgain ;
char Repeat ;

cout << " " << endl ;
cout << "Body Mass Index (BMI) Calculator" << endl ;
cout << " " << endl ;
cout << "Enter your weight in pounds. (please round to nearest whole number): " << endl ;
cin >> weight ;

cout << "Enter your height in inches. (please round to nearest whole number): " << endl ;
cin >> height ;

cout << "Your BMI is" << BMI << endl ;

if (BMI < 18.5)
cout << "You are underweight, and should considered gaining weight."

if (BMI <= 24.9)
cout << "Congratulations, you are a healthy weight."
"You should continue to maintain this weight" << endl ;

if (BMI <= 29.9)
cout << "You are over weight and should consider losing a little bit of weight." << endl ;

if (BMI <= 39.9)
cout << "You are obese and your weight could lead to serious health problems."

if (BMI >= 40.0)
cout << "You are severely obese and your weight may already be causing serious health complications."

cout << "Would you like to enter a new height and weight? (Y or N): " << endl ;
cin >> Repeat ;
if (Repeat = 'n')
DoItAgain = false ;

return 0 ;
}
#include <iostream>
using namespace std ;

int BMI(int weight, int height) ;
{
int wgt_kg ; // weight in kilograms
int hgt_m ; // height in meters

wgt_kg = (weight * 0.454) ; // kilograms = pounds * 0.454
hgt_m = (height * 0.0254) ; // meters = inches * 0.0254)
BMI = (wgt_kg/(hgt_m^2)) ; // BMI = kilograms/(meters^2)

return 0 ;
}
``````

Edited by Nick Evan: Fixed formatting

Also, why did you give the function the same name as the variable? You need to give them different names.

``````return 0 ;
}
#include <iostream>
using namespace std ;

int BMI(int weight, int height) ;
{``````

You don't need another #include .... using unless you put the function in another separate file. If they're together in one file then delete that.

Also, the declaration is:
int BMI(int weight, int height) ;

but the definition is:
int BMI(int weight, int height) {}

Notice that the definition has no semicolon at the end

BMI = (wgt_kg/(hgt_m^2)) ; // BMI = kilograms/(meters^2)

This is in your function, but the variable is in main. You cannot use variables from main in the function. They are separate units and do not see the variables of the other unless they are passed to the function. You should do the calculation in your function and then return the result of the calculation to main. In main there should be a line like so:

BMI = BMIFunction(weight, height);

This line assigns the variable BMI the returning value of the function. You need to replace BMIFunction() with the proper name of the function, of course.

So,let's say I name the Function BMICalc

Do I use that in the statement before the int main () at the top or the variable?

Also, when I call up the function, I would then use it's name, but how do I "name" it then?

the variable is BMI(int weight, int height) ??

``````#include <iostream>

using namespace std;

int BMIF(int weight, int height);

int main()
{
int weight ; // weight in pounds
int height ; // height in inches
float BMI ; // Body Mass Index
bool DoItAgain ;
char Repeat ;

cout << " " << endl ;
cout << "Body Mass Index (BMI) Calculator" << endl ;
cout << " " << endl ;
cout << "Enter your weight in pounds. (please round to nearest whole number): " << endl ;
cin >> weight ;

cout << "Enter your height in inches. (please round to nearest whole number): " << endl ;
cin >> height ;

BMI = BMIF(weight, height);
cout << "Your BMI is" << BMI << endl ;

if (BMI < 18.5)
cout << "You are underweight, and should considered gaining weight."

if (BMI <= 24.9)
cout << "Congratulations, you are a healthy weight."
"You should continue to maintain this weight" << endl ;

if (BMI <= 29.9)
cout << "You are over weight and should consider losing a little bit of weight." << endl ;

if (BMI <= 39.9)
cout << "You are obese and your weight could lead to serious health problems."

if (BMI >= 40.0)
cout << "You are severely obese and your weight may already be causing serious health complications."

cout << "Would you like to enter a new height and weight? (Y or N): " << endl ;
cin >> Repeat ;
if (Repeat = 'n')
DoItAgain = false ;

return 0 ;
}

int BMIF(int weight, int height)
{
int wgt_kg ; // weight in kilograms
int hgt_m ; // height in meters

wgt_kg = (weight * 0.454) ; // kilograms = pounds * 0.454
hgt_m = (height * 0.0254) ; // meters = inches * 0.0254)
return (wgt_kg/(hgt_m^2)) ; // BMI = kilograms/(meters^2)

}``````

This will compile, but check your calculations. I come up as underweight, but I'm not.... at least I don't think I am. ;)

Thank you for the help. I think I finally see my problem.

I did go ahead and switch to doubles, but this is only a basic intro to programming class.

I have a function program now, I only have to find 1 more solution I have left out.

thank you SO much for your time & help tonight.

Sara:cool:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.