In my classes, my damage script does not work. I think it might be because classes cant access other classes? I dont know, my book only had a 2 chapters over classes. I do these daily programs I make up just to help me out later on. There are two things I need help with.
1) How to get the damage() and hurt() functions to work.
2) Is there a way to make it one answer for the if(answer == "Yes" || answer == "yes" || answer == "YES")? I tried using toupper(), but it didnt work. (My book only mentioned what toupper did, not how to use it). This is how i used it: answer = (string)toupper();.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
class Player
{
int health = 100;
int Energ = 100;
public:
int GetHealth();
void damage(constint& x);
int GetEnergy();
private:
int pHealth = health;
int Energy = Energ;
};
int Player::GetHealth()
{
return pHealth;
}
void Player::damage(constint& x)
{
srand(time(0));
int num = x;
int y = rand()%20;
num = num - y;
Energy = Energy - 10;
}
int Player::GetEnergy()
{
return Energy;
}
class Enemy
{
int heal = 100;
public:
void hurt(constint& y);
int GetHeal();
private:
int eHeal = heal;
};
void Enemy::hurt(constint& y)
{
srand(time(0));
int r = y;
int d = rand()%20;
r = r - d;
}
int Enemy::GetHeal()
{
return eHeal;
}
int main()
{
Player Ron;
Enemy Obama;
int x = Obama.GetHeal();
int R = Ron.GetHealth();
cout << "\t\t\t\tTesting\n\n";
string answer;
do
{
cout << "\nYour Health is " << Ron.GetHealth() << ", and your Energy is " << Ron.GetEnergy();
cout << "\nObama's health is " << Obama.GetHeal();
cout << "\n\nWhat would you like to do? 1] Attack. 2] Run. 3] Surrender\n";
cin >> answer;
if(answer == "1")
{
if(Ron.GetEnergy()<=0)
{
cout << "\n\nYou ran out of energy, and have been killed by Obama\n\n";
break;
}
string answer2;
cout << "\n\nAre you sure you want to attack?\n";
cin >> answer2;
if(answer2 == "Yes" || answer2 == "yes" || answer2 == "YES")
{
Ron.damage(x);
cout << "\nYou attacked Obama.\n";
}
elseif(answer2 == "No" || answer2 == "NO" || answer2 == "no")
{
}
else
{
cout << "\nThat was not a valid option\n";
}
}
if(answer == "2")
{
string answer3;
cout << "\n\nAre you sure you want to run?\n";
cin >> answer3;
if(answer3 == "Yes" || answer3 == "yes" || answer3 == "YES")
{
if(Ron.GetHealth() > Obama.GetHeal())
{
cout << "\n\nYou've ran away successfully!\n\n";
cout << "The End\n\n";
break;
}
elseif(Ron.GetHealth() == Obama.GetHeal())
{
int luck;
luck = 1+rand()%2;
if(luck == 1);
{
cout << "\nYou escaped!\n";
cout << "\nThe End\n\n";
break;
}
if(luck == 2)
{
cout << "\Obama caught you.\nTheEnd?\n\n";
break;
}
}
elseif(Ron.GetHealth() < Obama.GetHeal())
{
cout << "\Obama caught you.\nTheEnd?\n\n";
break;
}
else
{
cout << "\n\nObama hacked the game?\n";
}
}
elseif(answer3 == "No" || answer3 == "NO" || answer3 == "no")
{
}
else
{
cout << "\nThat was not a valid option\n";
}
}
if(answer == "3")
{
string answer4;
cout << "\n\nAre you sure you want to Surrender?\n";
cin >> answer4;
if(answer4 == "Yes" || answer4 == "yes" || answer4 == "YES")
{
cout << "Obama captured you\n";
break;
}
elseif(answer4 == "No" || answer4 == "NO" || answer4 == "no")
{
}
else
{
cout << "\nThat was not a valid option\n";
}
}
Obama.hurt(R);
}while(x>0);
if(Obama.GetHeal()<=0)
{
cout << "\n\nYou killed Obama!\n\nYou Won!\n\n";
}
else
{
cout << "\nThanks for playing!";
}
}
Thanks, and if you find something that I shouldnt do in my program, please let me know. I'm trying really hard to become a good programmer in the future.