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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.