Why I got this functions wrong?

February 23, 2009 at 19:28:55
Specs: Windows Vista
Here is my function...

/* This program to help a small fitness center produce baseline information for new customers.
It inputs baseline information for each customer, and calculates target heart rates and BMI values
for the customer.

Inputs: first name
age
height in inches
weight

Outputs: first name
age
height in feet and inches
calculated BMI
starting target heart rate
final target heart rate

Author: May Nguyen
Date: 02/22/2009
*/

#include <iostream>
#include <string>
using namespace std;

// function prototypes
float calcBMI (float, int);
float calcMHR (int);
float calcStartHeart (float);
float calcFinalHeart (float);
void displayCusBaselineData (float, int, float, float);

int main()
{
string firstName; // customer's first name
int cusAge, // customer's age
heightInches, // customer's height in inches
heightFeet, // customer's height in feet
remainingInches; // remaining inches
float cusWeight, // customer's weight
cusBMI, // calculated body mass index
cusMHR, // calculated maximum heart rate
cusStartHeartRate, // calculated starting heart rate
cusFinalHeartRate; // calculated final heart rate

cusBMI = calcBMI (cusWeight, heightInches);
cusMHR = calcMHR (cusAge);
cusStartHeartRate = calcStartHeart (cusMHR);
cusFinalHeartRate = calcFinalHeart (cusMHR);
displayCusBaselineData (cusBMI, cusMHR, cusStartHeartRate, cusFinalHeartRate);

return 0;
}

string inputfirstName ()
{
string firstName;
cout << "Please enter your first name: " << endl;
cin >> firstName;

return firstName;
}

int inputCusAge ()
{
int cusAge;
cout << "Please enter you age: " << endl;
cin >> cusAge;

return cusAge;
}

int inputCusWeight ()
{
int cusWeight;
cout << "Please enter your weight: " << endl;
cin >> cusWeight;

return cusWeight;
}

int inputHeightInInches ()
{
int heightInches;
cout << "Please enter your heigth in inches: " << endl;
cin >> heightInches;
return heightInches;
}

// Calculted maximum heart rate
// Receives: cusAge
// Returns: cusMHR
float calcMHR (float cusAge)
{
const float HEART_START_VALUE= 220;
float cusMHR;

cusMHR = HEART_START_VALUE - cusAge;

return cusMHR;
}
// end calcMHR

// Calculated starting heart rate
// Receives: cusMHR
// Returns: cusStartHeartRate
float calcstartHeart (float cusMHR)
{
const float START_TARGET_MULTIPLIER = .65;
float cusStartHeartRate;

cusStartHeartRate = START_TARGET_MULTIPLIER * cusMHR;

return cusStartHeartRate;
}

// Calculated final heart rate
// Receives: cusMHR
// Returns: cusFinalHeartRate
float calcFinalHeart (float cusMHR)
{
const float FINAL_TARGET_MULTIPLIER = .85;
float cusFinalHeartRate;

cusFinalHeartRate = FINAL_TARGET_MULTIPLIER * cusMHR;

return cusFinalHeartRate;
}
// end calcFinalHeart

// Calculated body mass index
// Receives: cusWeight, heightInches
// Returns: cusBMI
float calcBMI (float cusWeight, float heightInInches)
{
const float BMI_MULTIPLIER = 703;
float cusBMI;

cusBMI = (cusWeight / (heightInches * heightInches)) * BMI_MULTIPLIER;

return cusBMI;
}

// Display calculated values
// Receives: cusMHR, cusStartHeartRate, cusFinalHeartRate, cusBMI
// Returns: none
void displayCusBaselineData (float cusBMI, float cusMHR, float cusStartHeartRate, float cusFinalHeartRate)
{
cout << " Maximum heart rate is: " << cusMHR << endl << endl;
cout << " Starting heart rate is: " << cusStartHeartRate << endl << endl;
cout << " Final heart rate is: " << cusFinalHeartRate << endl << endl;
cout << " Body mass index is: " << cusBMI << endl;
}
// displayCusBaseline


See More: Why I got this functions wrong?

Report •


#1
February 25, 2009 at 12:12:08
It's looks like you've got a few data type declaration problems, naming convention issues, and using un-initialized data problems.

cusBMI = calcBMI(custWeight, heightInches);

you've got two issues there. 1) you're using un-initialized data as custWeight and heightInches haven't been assigned anything yet, and 2) your prototype doesn't match the definition.

float calcBMI(float, int); // prototype.
cusBMI = calcBMI(cusWeight, heightInches); // caller
float calcBMI(float cusWeight, float heightInInches) { ... } // definition

Make sure you initialize data before trying to use it - you've declared functions for that, but the best I could tell, you never called them from main or from within your first function call calcBMI; check your naming conventions within functions; make sure your data types match in function prototypes, calls, and definitions.

HTH


Report •
Related Solutions


Ask Question