   C++ classes problem
  i have this small health calculator programit worked fine without using classes but iwanted to incorporate OOP for neatness andfor my own learning.The program compilesfine but the output is not correct.Here is the source#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()   {}  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;}if i select choice 1it only asks me to enter heightand if i enter height as 120i get 1.38646e+010(PS:i do not know how to post a screen shot in the forum)(please run the source code to see the output)please help me out.   
  Just as a general tip, when you have int choice; you should always get into the habit of declaring it.  I was unable to run your source unless I changed int choice to int choice = 0;  For me it sets choice to -858993460 without declaring.When you picked option 1, what weight and age did you put in?  I put in 120 for height and received nothing like that.   ;  You have to initialize HeartRate, Age, Height and Bodyweight in your constructor.If you do not they contain any value (I think in Debug build they get initiaized to 0 with Visual Studio IIRC).Something likeBodyState::BodyState()  :  HeartRate(0),Age(0),Height(0),Bodyweight(0){}Hope that helps.Edit:If you don't want to initialize your variables then you don't need to write the constructor. The compiler will generate it for you (as for assignment operator and copy constructor).Rule of thumb: Always initialize your variables.;  Just make it a habit with everything to declare, never leave anything like int something; lying around.  Always set it, even to 0.   ;  thanks for the quick replies guys now i know that i must initialise variables Haben Sie vielen Dank! to megamoscha i modified theconstructor as you suggested and it worked instantly;Quote:but iwanted to incorporate OOP for neatness andfor my own learning.This isn't, so far, at all how OOP programs are supposed to be organized. Time to see how neatness works, and learn by example. :)Don't construct an object until you have all the information you need to put the object into a meaningful state. Then, initialize the object with all that data, by using a constructor which accepts the information. No-argument constructors are for classes that can reasonably be initialized with no outside information into some "default" state.Don't use the object to perform I/O, unless some kind of I/O functionality is the stated purpose of the class. You don't do input in the member functions because, just as with ordinary functions, you are supposed to receive the information you need from (a) the surrounding context (existing data members of the class) and (b) parameters. Similarly, you don't do output there because that's what the return value is for.The I/O belongs to the code that is using the class. That way, you gain flexibility (the using-code can decide which streams to use - maybe a file instead of std::cin and std::cout, for example - or even get/display information in a completely different way) and organization (separation of responsibility: it becomes clear what everything's purpose is, and the purposes make sense according to the names of the classes and functions).Look for opportunities to reuse code. Reading in the data is fundamentally the same process each time: you loop until the user provides a positive number. To accomplish this, we can make a function that accepts a prompt and an error message, performs the loop (using the supplied text), and returns the user's input when it is valid.Finally, don't use class members to store derived calculation results. Basically, the usual general principle applies: restrict variable scope where possible. If you can do it with a local variable (or no variable at all!), don't use a data member. The data members are there, theoretically, to represent the actual state of the object, and practically, to share data between member functions of the same object.Some additional small comments are made in the resulting code below.// Nothing in this code actually uses <cstdlib>.#include <iostream>// We will need this in order to pass text around between functions easily:#include <string>using namespace std;// A helper function for getting positive numbers from the user.int getInput(int min, int max, const string& prompt, const string& error) {// Try to be consistent with spacing. A usual approach is to use either// some specific number of spaces, or one tab, for indents, and use the// same amount every time. In this code, I use a tab.int result = min - 1;// I initialize to something unacceptable to be sure that the loop runs.while (result < min || result > max) {// See how the multiple indents work?cout << prompt;cin >> result;if (result < min || result > max) {cout << error << endl;}}return result;}class Body {public:// We need a little data type first...enum sex { MALE, FEMALE };// We can use an "initialization list" to implement all of the constructor// logic. This is usually the case. The body of the constructor is only for// "special" initialization work. Just copying data into the members is easy.Body(int Age, int Height, int Weight, sex Sex):Age(Age), Height(Height), Weight(Weight), Sex(Sex) {}// Notice that the constructor is already implemented here, so I won't do// it outside the class.// We use the return value to give the information back to the main// function, which will display it.int HeartRate();double RMR();double MinimumWater();// Stats() had nothing to do with the Body, so I took it out and did the// work directly outside.// There was no Choice() function, so I removed the declaration.private:// Keep data 'private' that shouldn't be touched directly by outside code.// Only these three from the original are "core" properties of the Body.int Age, Height, Weight; // "Bodyweight" is redundant.// But we should add one more:sex Sex;// That is, we define a type 'sex', and declare a member 'Sex' of that type.// Another approach is to use a boolean, but this way is clearer.// Anyway, this is added because it doesn't make sense to ask about the// RMR-for-men of a woman or vice versa.};int Body::HeartRate() {return 220 - Age; }double Body::RMR() {if (Sex == MALE) {return 66 + (13.7 * Weight) + (5 * Height) - (6.8 * Age);} else { // must be FEMALE.return 655 + (9.6 * Weight) + (1.8 * Height) - (4.7 * Age);}}double Body::MinimumWater() {// I collapsed all the multiplications and divisions together.return Weight / 31.8; // Approximately.}int main(int argc, char *argv[]) {// First, we find out some information about the user.int age = getInput(5, 100, "Enter your age: ", "Please Enter a valid age");int height = getInput(0, 250, "Enter your Height(CM): ", "Please Enter a valid height");int weight = getInput(0, 200, "Enter your Weight(KG): ", "Please Enter a valid weight");// You can't read directly into an enumeration type, so we'll have to ask// the user indirectly.Body::sex sex = Body::MALE;char sex_letter;cout << "Are you male or female? (M/F) ";cin >> sex_letter;if (sex_letter == 'f' || sex_letter == 'F') { sex = Body::FEMALE; }// Now we can construct the Body.Body body(age, height, weight, sex);int choice = 0; // Again, initialize things first :)while (choice != 5) {// Space things out a little. It's easier on the eyes.cout << "1) RMR" << endl;cout << "2) Optimal Heart Rate" << endl;cout << "3) Minimum Water" << endl;cout << "4) Energy Expenditure" << endl;cout << "5) Quit" << endl;cout << "Enter your choice: ";cin >> choice;switch (choice) {case 1:// What are the units? Calories per hour?cout << "Your RMR is " << body.RMR() << endl;break;case 2:cout << "Your optimal heart rate is " << body.HeartRate() << endl;break;case 3:cout << "Your optimal water intake should be " << body.MinimumWater() << " liters" << endl;break;case 4:cout << "Calories Per Gram" << endl;// Pretty sure this should be 9, not 8 :)cout << "1 gram fat = 9 calories" << endl;cout << "1 gram carbohydrate = 4 calories" << endl;cout << "1 gram protein = 4 calories" << endl;break;// We don't need to handle the 'exit' case because it already// breaks the loop.}}// Don't pause programs artificially at the end. Also, there is no need// to return 0 explicitly from main() - it's a special case.}