Thread: classes problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    classes problem

    hi this is my first post and i have a problem with classes
    i have made a small health meter
    however the choices are not independent
    for example if i select choice one and enter the values it works fine
    but if i now select choice two it uses the same values of choice one
    and just gives an output without letting me enter the values

    Here is the source code

    Code:
    #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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem I see is that you are basically just creating several global variables disguised as member variables in a class. Many of your member functions just coincidentally use the member variables without really being part of a coherent class interface.

    If you are always going to ask the user to enter specific data and then compute based on that data, then perhaps you just need to do this procedurally with a few functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    If you create your object inside your while loop in the main(),then you can call your BodyStats constructor and thus making all the field variables again set to 0,thus re-initializing them.
    Code:
    .
    .
    .
    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;
        
           BodyStats Body;  //<<
          .
          .
          cout<<"Enter your choice"<<endl;
            cin>>choice;
            
          
          switch(choice)
          {
           .
           .
          //<<may be here a 
         //default: cout <<"Invalid Input"; 
          }
    Last edited by zalezog; 05-13-2009 at 04:40 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zalezog View Post
    If you create your object inside your while loop in the main(),then you can call your BodyStats constructor and thus making all the field variables again set to 0,thus re-initializing them.
    True, but I don't think that's the right solution, like laserlight says, the problem is much larger than the initialization of member variables - it's a case of class design and usage of the class.

    I think it's nearly always wrong to use cin/cout inside member functions. There are exceptions, but in general, interaction with the user in relation to objects do not normally belong inside member functions.


    On a pedantic side-note:
    I don't think "optimal heart rate" is the correct term for the result of 220-age. The correct term is probably "approximate maximum heart rate". The actual way to find a maximum heart-rate is to do a (very painful) max heart-rate test, which involves pushing the heart rate to a maximum rate [for anyone over the age of 25, this should probably be done under supervision of someone with at least a bit of medical training].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM