C++ classes

hi my problem is as follows
i have a program that calculates various
bodily functions. Im using classes for this.
if i choose option one the program
works fine.
However if i choosee another option say choice 2
the program just gives output using the values
that were entered before in choice one
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

#include <cstdlib>
#include <iostream>

using namespace std;

         ////////////////
class BodyStats
{
      public:
             BodyStats();
            //~BodyStats(); 
            int HeartRate,Age,Height,Bodyweight;
            
             double Bweight,LitersWater,MinWater,RMR;
         
            void FindHeartRate();
            void FindMenRMR();
            void FindFemaleRMR();
            void FindMinimumWater();
            void Stats();
            void Choice();
    /*void FindEnergyExpenditure()
         {
         cout<<"Enter your RMR"<<endl;
         cin<<RMR;
         cout<<
         }*/
         };
         
   BodyStats::BodyStats()
    :  HeartRate(0),
     Age(0),
     Height(0),
     Bodyweight(0)
   {}
  void BodyStats::FindHeartRate()        
    {
      while(Age<=0)
      {
          cout<<"Enter your age";
          cin>>Age;
            if(Age<=0)
               cout<<"Please Enter a valid age"<<endl;
      }
     HeartRate = 220-Age; 
     cout<<"your optimal heart rate is"<<HeartRate;
     
     }
     
  void BodyStats::FindMenRMR()
     {
                   
         while(Bodyweight<=0)
               {
                  cout<<"Enter your Weight(KG)";
                  cin>>Bodyweight;
                if(Bodyweight<=0)
                  cout<<"Please Enter a valid weight"<<endl;
                   cout<<""<<endl;
                }
           while(Height <= 0)
                {
                    cout<<"Enter your Height(CM)";
                    cin>>Height;
                  if(Height <= 0)
                    cout<<"Please Enter a valid height"<<endl;
                     cout<<""<<endl;
                 }
           while(Age <= 0) 
                {              
                       cout<<"Enter your age";
                       cin>>Age;
                    if(Age <= 0)
                        cout<<"Please Enter a valid age"<<endl;
                         cout<<""<<endl;
                }
                       RMR = 66 + (13.7 * Bodyweight)+(5 * Height)-(6.8 * Age);
                       cout<<"Your RMR is = "<<RMR<<endl;
                       
                      
                
       }
   void BodyStats::FindFemaleRMR()
      {
        while(Bodyweight<= 0)
        {
       cout<<"Enter your Weight(KG)"<<endl;
       cin>>Bodyweight;
        if(Bodyweight <= 0 || Bodyweight>=200)
        cout<<"Enter a valid weight"<<endl;
        }
        while(Height<= 0 || Height >= 250 )
        {
         
             cout<<"Enter your Height(CM)"<<""<<endl;
             cin>>Height;
          if (Height<= 0 || Height >= 250)
             cout<<"Enter a valid height"<<endl;
          }
          
          while(Age<=5 || Age>=100) 
          {
       cout<<"Enter your age"<<""<<endl;
       cin>>Age;
          if (Age<=5 || Age>=100)
          cout<<"Enter a valid age"<<endl;
          }
       RMR = 655 + (9.6 * Bodyweight)+(1.8 * Height)-(4.7 * Age);
        
        cout<<" "<<endl;
        
       cout<<"Your RMR is" <<""<<RMR<<endl;
       cout<<" "<<endl;
      
      
       }  
   void BodyStats::FindMinimumWater()
       {
        cout<<"Enter Bodyweight"<<endl;
        cin>>Bodyweight;

        Bweight = Bodyweight * 2.2;
        MinWater = Bweight / 2;
        LitersWater = MinWater/35;
        cout<<"Your optimal water intake should be"<<LitersWater<<"liters"<<endl;
        
        }    
    void BodyStats::Stats()
        {
          cout<<"Calories Per Gram"<<endl;
          cout<<"1 gram fat = 8 calories"<<endl;
          cout<<"1 gram carbohydrate = 4 calories"<<endl;
          cout<<"1 gram protein = 4 calories"<<endl;     
         }    
         
       
int main(int argc, char *argv[])
{
      
      BodyStats Body;
      int choice; 
      while(choice!=6) 
     {
      
      cout<<"1)Men RMR"<<endl;
      cout<<"2)Women RMR"<<endl;
      cout<<"3)Find Optimal Heart Rate"<<endl;
      cout<<"4)Minimum Water"<<endl;
      cout<<"5)Energy Expenditure"<<endl;
      cout<<"6)Quit"<<endl;
    
    
        cout<<"Enter your choice"<<endl;
        cin>>choice;
        
      
      switch(choice)
      {
            case 1:        
               Body.FindMenRMR();
               break;
            case 2:   
                Body.FindFemaleRMR();
               break;
            case 3:   
               Body.FindHeartRate();
                break;
            case 4:   
               Body.FindMinimumWater();
               break; 
            case 5:     
               Body.Stats();
               break;
            case 6:
                 return 0;
              break;   
      }  
    
}

    system("PAUSE");
    return 0;
}


please help out. Thanks

PS:Iam using DevC++
The problem is, in order to verify correct input for BodyStats::FindFemaleRMR() you test the condition while(Bodyweight<= 0), and since you already ran the program once using the male version, you set Bodyweight to be something above 0. So when you run it again for the female, it fails that condition for the while, and goes straight to the result.
Side Note: you've got some warnings in this code, make sure you compile at the highest warning level and get rid of those.
Last edited on
Topic archived. No new replies allowed.