C++ POO of a character with options to hit and heal

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;
}

This is a long code and giving hints seems a little hard so I will just send a whole chunk of code. I hope it helps fix your mistakes and learn from them.

#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;
};

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 (option != 'c');
	
	return 0;
}

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){
    return HP <= 0;
}

This code only help you fix all the errors you are getting so that you can continue working on it.

Errors usually tell you what is wrong in the code.
Edit: sorry I missed the picture.

So the error messages point toward lines where there are wrong or missing characters. That’s all Syntax.

For example, Character::getName is just lacking the closing “}”.
Please take the message, go to the lines and look for missing characters.
After that, try again and see for new errors. Also please provide a better view on the errors - I think the numbers refer to the lines where the error happened but you didn’t include the head for the table…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.