I been working on some code examples to practice on DevC++ and I got an exercise and got stuck on this one and been getting several errors,
it just says have a class of Character with name and hp, with 5 methods of
Hit : for the character to lose hp
Alive: which return true or false if depending of how much hp it has
getHp: returns the value of the private attribute of hp
getName: returns the value of the private attribute of the name
eat: which heals a certain amount of hp
been watching tutorials and examples but can’t seem to get this one to work, the examples I found are mostly a combat system with one character hitting another for a set amount with is a random number
#include <iostream>
#include <string>
using namespace std;
class Character {
public:
Character(string name, double HP);
double getHP();
string getName();
void HPgain(double eat);
void HPlost(double getHit);
// bool Alive(double h_p);
private:
string name;
double HP;
};
Character::Character(string name, double HP){
if (HP <0){
this->HP = 0;
} else {
this->HP = HP;
}
this->name = name;
}
double Character::getHP(){
return this->HP;
}
string Character::getName(){
return this->name;
void Character::HPgain(double eat){
this->HP += eat;
}
void Character::HPlost(double getHit){
this->HP -= getHit;
}
//bool Character::Alive(double h_p){
// if (HP <= 0){
// return false;
// }
// return true;
// }
//}
int main(){
Character myCharacter("Cyrus sarknym", 400);
cout << " --------------------- " << endl;
cout << "Name of Character: " << myCharacter.getName << endl;
cout << "HP: " << myCharacter.getHP << endl;
cout << " --------------------- " << endl;
double hit = 50;
double bread = 100;
char option;
do {
cout << "Action to take: a) Eat b) Hit c) End" << endl;
cin >> option;
switch (option){
case 'a':{
cout << "Eating and healing hp: ";
myCharacter.HPgain(bread);
cout << " --------------------- " << endl;
cout << "Name of Character: " << myCharacter.getName << endl;
cout << "HP: " << myCharacter.getHP << endl;
cout << " --------------------- " << endl;
break;
}
case 'b':{
cout << "Comiendo y recuperando puntos de vida: ";
myCharacter.HPlost(hit);
cout << " --------------------- " << endl;
cout << "Name of Character: " << myCharacter.getName << endl;
cout << "HP: " << myCharacter.getHP << endl;
cout << " --------------------- " << endl;
// cout << "Got hit and lost your HP ";
// bool response = myCharacter..Alive();
// if (response == false){
// cout << name << " lost all of its hp, he has died." << endl;
// }
break;
}
}
} while (opcion != 'c');
return 0;
}