Challenge 8

Description

The purpose of this challenge is to use overloaded functions and to use default parameters. This challenge will calculate BMI (Body Mass Index).

Requirements

  1. Create a function, bool use_metric() to ask the user if metric units will be used. This function returns a true/false value depending on what the user inputs. You can write a cout/cin combination in this function to get the user’s input.
  2. Create a function, double calc_BMI(double height, double weight, bool is_metric = true). The is_metric parameter must be declared as a default parameter set to true. Return the result of your BMI calculation out of the function.
  3. BMI is calculated as:
    In metric, (mass in kg) / (height in m)2
    
  4. Write two overloaded functions to converts units
    // the value parameter represents a height or a weight value
    // these functions convert imperial values to metric
    // use values for unit_type of 1 = length and 2 = weight
    double convert_units(double value); // always LENGTH conversion
    double convert_units(double value, int unit_type); LENGTH or WEIGHT conversion
    
    // sample calls
    height = convert_units(height); // convert height to metric
    weight = convert_units(weight, 2); // convert weight to metric
    

    The calc_BMI() function will call convert_units() to ensure that BMI is calculated using metric data since BMI must be calculated with metric values.

    The convert_unit() functions will convert value—from imperial to metric—using a length or weight conversion depending on unit_type. value can represent either a height or weight. Use these conversion rates in the function:

    1 meter = 39.3701 inches (LENGTH conversions)
    1 kg = 2.20462 lb.  (WEIGHT conversions)
    
  5. Ask the user for a height value and a weight value.
  6. Calculate the BMI using the function in #2. If the user will use metric, call the calc_BMI() function without the bool variable parameter. If the user will use imperial, pass in a value of false in the last parameter of the function.
  7. Display the results to the user.

 Sample main()

int main()
{
  double height, weight, bmi;
  if (use_metric())
  {
    cout << "Okay, using metric...\n";
    cout << "What is your height in meters? ";
    cin >> height;
    cout << "What is your weight in kg? ";
    cin >> weight;

    // call to function with default parameter
    // notice only 2 parameters?
    bmi = calc_BMI(height, weight);
  }
  else
  {
    cout << "Okay, using imperial...\n";

    cout << "What is your height in inches? ";
    cin >> height;
    cout << "What is your weight in pounds? ";
    cin >> weight;

    // call to function with default parameter
    // notice 3 parameters? 
    bmi = calc_BMI(height, weight, false);
  }

  // you may have to use setprecision() 
  cout << "Your BMI is " << bmi << endl;

  return 0;
}

Sample Interaction / Output

$./a.out
Do you want to use metric measurements (Y/N)? Y
Okay, using metric…

What is your height in meters? 1.5
What is your weight in kg? 80

Your BMI is 35.55

Run it again

$./a.out
Do you want to use metric measurements (Y/N)? N
Okay, using imperial…

What is your height in inches? 59.055
What is your weight in lbs? 176.37

Your BMI is 35.55

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0008

Print Requirements