### BMI calculator!!

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include using namespace std; class bmi { public: virtual float 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.

Thanks.
Last edited on
You needed a return value.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include using namespace std; class bmi { public: virtual float 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; }``````
Last edited on
oh my gosh, I feel really stupid now.
`}while(height < 1 && weight < 1);`

1 inch & 1pound !!
 }while(height < 1 && weight < 1); 1 inch & 1pound !!

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'.

:D
Last edited on
It's the simple things that escape your notice. I just recently had an issue with file io, and I solved it by opening it the right way. :P
 ``1234567891011121314151617181920212223242526`` ``````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; } else if ( 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; } else if ( 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?
Topic archived. No new replies allowed.