C++ Primer Plus Chapter 3 Exercise 2

c plus plusExercise 2 was a little more involving as far as conversions go. Nevertheless, I was able to find a solution to this:

2. Write a short program that asks for your height in feet and inches and your weight in
pounds. (Use three variables to store the information.) Have the program report your
body mass index (BMI). To calculate the BMI, first convert your height in feet and inches
to your height in inches (1 foot = 12 inches). Then, convert your height in inches to
your height in meters by multiplying by 0.0254. Then, convert your weight in pounds
into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing
your mass in kilograms by the square of your height in meters. Use symbolic constants
to represent the various conversion factors.

#include <iostream>

using namespace std;

int main()
{
const int foot = 12;
const double meter = 0.0254;
const double kilo = 2.2;
int feet;
int inches;
int pounds;
int heightInches;
double weightKG;
double meters;
double bmi;

// gather input
cout << "Enter your height in feet: ";
cin >> feet;

cout << "Enter your height in inches: ";
cin >> inches;

cout << "Enter your weight in pounds: ";
cin >> pounds;

// Calculate BMI
heightInches = feet * foot + inches;
meters = heightInches * meter;
weightKG = pounds / kilo;
bmi = weightKG / (meters * meters);

cout << "Your Body Mass Index(BMI) is: " << bmi << endl;

return 0;
}