I was wondering if someone can tell me if I did this exercise from C++ primer right. I'm not sure if I did. I tried to follow along with the exercise the best I could, but not being the best at math has left me to not knowing if there is a logical error, sense the program runs fine..
#include<iostream>
#include<cmath>
usingnamespace std;
int main()
{
// int variables for holding user data
int inches;
int feet;
int weight;
// ask for user input
cout << "Please enter your height in feet: ";
cin >> feet;
cout << endl;
cout << "Please enter your height in inches: ";
cin >> feet;
cout << endl;
cout << "Please enter your weight: ";
cin >> weight;
// conversions are done below
int FeetInInches;
double InchesInMeters;
double MassKG;
FeetInInches = (feet / 12) + inches;
InchesInMeters = inches * 0.0254;
MassKG = weight * 2.2;
//calculate body mass
double BMI;
BMI = MassKG / sqrt(InchesInMeters);
// display Body Mass Index
cout << "Your Body Mass Index Is: " << BMI;
return 0;
}
#include<iostream>
#include<cmath>
usingnamespace std;
int main()
{
// int variables for holding user data
int inches;
int feet;
int weight;
// ask for user input
cout << "Please enter your height in feet: ";
cin >> feet;
cout << endl;
cout << "Please enter your height in inches: ";
cin >> inches;//inches here, not feet
cout << endl;
cout << "Please enter your weight: ";//specify lbs?
cin >> weight;
// conversions are done below
int FeetInInches;
double InchesInMeters;
double MassKG;
FeetInInches = (feet * 12) + inches;//As Moschops said
InchesInMeters = FeetInInches * 0.0254;//Convert the whole thing, not just the inches
MassKG = weight * 2.2;
//calculate body mass
double BMI;
BMI = MassKG / sqrt(InchesInMeters);
// display Body Mass Index
cout << "Your Body Mass Index Is: " << BMI;
return 0;
}
Can only get better I guess. I should have done what Moschops pointed out by running a 1 foot converstion test. I'll have to be weary of that when converting in the future to make sure I have the formula right.
Can't believe I did the to meters conversion, and forgot to include the feet in there, lol, that was a waste. haha.