classes problem
May 13, 2009 at 9:22am UTC
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
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
#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
May 13, 2009 at 10:25am UTC
haha that 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
enum life {verygood = '1' , good, bad, sick, ohno};
int main()
{
using namespace std;
char health = 'd' ;
cout << "Talk program\n" ;
cout << "Type in 1 for good to 5 for bad\n" ;
for (;;)
{
cin >> health;
switch (health)
{
case verygood:
cout << "You are good as grass\n" ;
break ;
case good:
cout << "Don't worry your gonna make it\n" ;
break ;
case bad:
cout << "You need to, goto the docs\n" ;
break ;
case sick:
cout << "Keep a bin around at all times\n" ;
break ;
case ohno:
cout << "Try praying to god\n" ;
break ;
case 'e' :
cout << "Exiting...\n" ;
return 0;
default :
cout << "Error, pick again" ;
break ;
}
}
return 0;
}
May 13, 2009 at 10:11pm UTC
I hope this helps
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
#include <iostream>
#include <string>
class HP
{
public :
HP(int L) { life = L; }
~HP(){}
std::string CHP(); // check life
void SHP(int s); // set life
void DHP(); // display life
private :
int life;
std::string lifebar;
};
std::string HP::CHP()
{
if (life == 100)
lifebar = "||||||||||" ;
if (life > 100)
lifebar = "|||||||||||||||||||||" ;
if (life <= 50)
lifebar = "|||||" ;
if (life < 20)
lifebar = "||" ;
return lifebar;
}
void HP::SHP(int s)
{
life = s;
}
void HP::DHP()
{
lifebar = CHP();
std::cout << "Life " << life << "% " << lifebar << "\n" ;
}
int main()
{
using namespace std;
HP kid(50);
kid.SHP(150);
kid.DHP();
return 0;
}
Topic archived. No new replies allowed.