okay. First thing first, I am a beginner who just got into ADTs and polymorphism. And second, the program is not complete, I just completed first part of it and then checked if it worked so I can have less debugging time later. So here is the code.
#include<iostream>
usingnamespace std;
class bmi {
public:
virtualfloat BmiCalculator() = 0;
};
/////////////////////////////////////////////////////////////////////////////////////////
class imperial: public bmi { // measures the Body Mass Indicator in imperial units.
float height_inch;
float weight_pounds;
public:
void setHeight(float h) {height_inch = h; };
void setWeight(float w) {weight_pounds = w; };
float BmiCalculator() { (weight_pounds * 703) / (height_inch * height_inch); } // the BMI
};
//////////////////////////////////////////////////imperial///////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
class metric: public bmi {
float height_metre;
float weight_kilo;
public:
void setHeight(float h) {height_metre = h;}
void setWeight(float w) {weight_kilo = w;}
float BmiCalculator() { weight_kilo / (height_metre * height_metre); } // the BMI
};
/////////////////////////////////////////////////metric////////////////////////////////////
int main() {
int choice;
while(true) {
cout << "this program calculates the BMI(Body Mass Index) to determine your health." << endl;
cout << "press 1 if you want to calculate BMI in imperial units." << endl;
cout << "press 2 if you want to calculate BMI in metric units." << endl;
cin >> choice;
if(choice == 1) {
imperial index;
float height;
float weight;
do { // doesn't compute unless the user inputs a number bigger than 0.
cout << "please enter a number that is bigger than 0." << endl;
cout << "type in weights in pounds(lb)" << endl;
cin >> weight;
cout << "type in heights in inches(in)" << endl;
cin >> height;
}while(height < 1 && weight < 1);
index.setHeight(height);
index.setWeight(weight);
bmi *result = &index;
cout << "your Body Mass Index is:" ;
cout << result -> BmiCalculator() << endl;
}
}
cin.get();
cin.get();
return 0;
}
As I said, only the first part is complete. so please press '1' if it asks between imperial and metric. Now, if you type in your pounds and heights and want the program to calculate the BMI, the program spits out complete gibberish. I want to know why, and also tell me how I can improve the program.
#include<iostream>
usingnamespace std;
class bmi {
public:
virtualfloat BmiCalculator() = 0;
};
/////////////////////////////////////////////////////////////////////////////////////////
class imperial: public bmi { // measures the Body Mass Indicator in imperial units.
float height_inch;
float weight_pounds;
public:
void setHeight(float h) {height_inch = h; };
void setWeight(float w) {weight_pounds = w; };
float BmiCalculator() {
return ((weight_pounds * 703) / (height_inch * height_inch)); // the BMI
}
};
//////////////////////////////////////////////////imperial///////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
class metric: public bmi {
float height_metre;
float weight_kilo;
public:
void setHeight(float h) {height_metre = h;}
void setWeight(float w) {weight_kilo = w;}
float BmiCalculator() { weight_kilo / (height_metre * height_metre); } // the BMI
};
/////////////////////////////////////////////////metric////////////////////////////////////
int main() {
int choice;
while(choice != 0) { // give the user a way to quit
cout << "this program calculates the BMI(Body Mass Index) to determine your health." << endl;
cout << "press 1 if you want to calculate BMI in imperial units." << endl;
cout << "press 2 if you want to calculate BMI in metric units." << endl;
cin >> choice;
if(choice == 1) {
imperial index;
float height;
float weight;
do { // doesn't compute unless the user inputs a number bigger than 0.
cout << "please enter a number that is bigger than 0." << endl;
cout << "type in weights in pounds(lb)" << endl;
cin >> weight;
cout << "type in heights in inches(in)" << endl;
cin >> height;
}while(height < 1 && weight < 1);
index.setHeight(height);
index.setWeight(weight);
bmi *result = &index;
cout << "your Body Mass Index is:" ;
cout << result -> BmiCalculator() << endl;
}
}
return 0;
}
BMI calculator for aliens. LOL
well yeah. As I said, I just did the basic bone for the program and I'm going to fix things. If I fix the first part of the program, I will research the average pounds and inches and then replace them with '1'.
cout << "your Body Mass Index is:" ;
cout << result -> BmiCalculator() << endl; // print
if( result -> BmiCalculator() < 18.5) { // rate of under 18.5 will make you underweight.
cout << "you're underweight." << endl;
}
elseif ( result -> BmiCalculator() >= 18.5 && result -> BmiCalculator() < 25) { // BMI of 18.5 - 24.9 will make you normal weight
cout << "you have a normal weight." << endl;
}
elseif ( result -> BmiCalculator() >= 25 && result -> BmiCalculator() < 30) { // BMI of 25 - 29.9 will make you overweight.
cout << "you're overweight." << endl;
}
else {
cout << "you're obese." << endl;
}
this is the code to compute how obese you are after the result. The thing is, I don't think it will be very efficient to write this code TWICE on BOTH imperial and metric parts. Is there a way so I can take in my 'result' for my functions so I can compute this?