Question on my code, Please look

Started by
11 comments, last by Psych0 P3ngu1n 13 years, 11 months ago

#include<iostream>
#include<string>
using namespace std;

class Spell
{
protected:
	int Mana;
public:
	bool SpellHit(int,int);
	bool Summon();
};

class Warrior
{
protected:
	int mana;
	
public:
	int health;
	Warrior();
	void SpellUse(Spell &SpellObj);
}*WarriorPtr;

Warrior::Warrior()
{
	mana = 10;
	health = 100;
}
class Ogre
{
protected:
	
	int mana;
public:
	int health;
	Ogre();
	void SummonTroll(Spell &SpellSummon);
}*OgrePtr;

Ogre::Ogre()
{
	health = 50;
	mana = 15;
}
class Troll : public Ogre
{
protected:
	int mana;
	
public:
	int health;
	Troll();
}*TrollPtr;

Troll::Troll()
{
	health = 150;
	mana = 50;
}
int main()
{
	Warrior Rob;
	Ogre Rick;
	Spell Fire;
	Spell CreateTroll;
	Rob.SpellUse(Fire);
	Rick.SummonTroll(CreateTroll);
	return 0;
}
bool Spell::SpellHit(int locX, int locY)
{
	int enemyLocX, enemyLocY;
	enemyLocX = 300;
	enemyLocY = 50;
	if((locX == enemyLocX)&&(locY ==enemyLocY))

		return true;
	else
		return false;
}
bool Spell::Summon()
{
	Ogre Rick;
	if(Rick.health < 60)

		return true;
	else
		return false;
}


void Warrior::SpellUse(Spell &SpellObj)
{
	Ogre Rick;
	bool hit;
	hit = SpellObj.SpellHit(300,50);

	if( hit )
		cout<<"Warrior cast a fire spell on the Ogre."<<endl<<endl;
		Rick.health -= 32;
		cout<<"Ogre's health reduced to "<<Rick.health<<"."<<endl<<endl;
		mana -= 10;
		cout<<"Mana by 10 reduced to "<<mana<<"."<<endl<<endl;
}

void Ogre::SummonTroll(Spell &SpellSummon)
{
	
	bool hit;
	hit = SpellSummon.Summon();

	if( hit )
		cout<<"A new Troll was created to help Ogre attack Warrior."<<endl<<endl;
		mana -= 15;
		cout<<"Mana reduced by 15 to "<<mana<<"."<<endl<<endl;
}

Ok, so first off this code works. My question comes in where I wrote bool Spell:Summon { Ogre Rick; if(Rick.health < 60) return true; else return false; } how would I make it so it can say (Rick.health < 50) because he was attacked and his health is already lower than max health. That way I don't have to write 60, because Ogre's health is maxed out at 50.
Advertisement
Why do you want to do that? I dont even understand what is problem... :(

Deltron Zero and Automator.

I don't have time to answer your question, but I saw one glaring mistake:

	if( hit ) { // You forgot the braces. Intendation does nothing in C++.		cout<<"A new Troll was created to help Ogre attack Warrior."<<endl<<endl;		mana -= 15;		cout<<"Mana reduced by 15 to "<<mana<<"."<<endl<<endl;         }
Are you aware that
bool Spell::Summon()
{
Ogre Rick;
if(Rick.health < 60)

return true;
else
return false;
}

this code will always return true since you are creating the Ogre Rick inside the fucntion and it is initializing health to 50.So that function will always return true.

I don't really understand what you are trying to do but if you want to check the health of an ogre in that method just pass it in as a parameter like :

bool Spell::Summon(const Ogre& ogre)
{

if(ogre.health < 60)

return true;
else
return false;
}
Quote:Original post by Konfusius
I don't have time to answer your question, but I saw one glaring mistake:

*** Source Snippet Removed ***


There are also another similar mistake above in his code

Deltron Zero and Automator.

Well i have it at 60 because when ogre is created it has 50 health, so it does return true. But I want to be able to write health<50 in Summon() because Ogre was previously attacked when ran on Visual Studio in C++
Sorry I meant true not false :)

You have a logical problem here.If you explain to us what summon is supposed to do then we can help more.Right now it justs returns true if health is smaller than 60.


I think you want something like this,
void Ogre::SummonTroll(Spell &SpellSummon){		bool hit;	hit = SpellSummon.Summon(this);	if( hit ){		cout<<"A new Troll was created to help Ogre attack Warrior."<<endl<<endl;		mana -= 15;		cout<<"Mana reduced by 15 to "<<mana<<"."<<endl<<endl;}}bool Spell:Summon(const Ogre* ogre){if(ogre->health < 60)return true;elsereturn false;}



You can't access the Ogre Rick you declared in main() by typing the same name in a method.Variables declared inside a function body are local to that method they are not the same as the Rick in main.
Ok Black Knight you just answered my question, sorta. Is there a way to make Rick the same Ogre Rick in the functions. Spell Summon() is for a lab for school on Association. Its makes Ogre use the spell (or something like that) is what my teacher says. Summon() is supposed to initiate and make Ogre Rick summon a troll.
The problem isn't that you're checking the health of the ogre. The problem is that you instantiate the object one line above where you check it's health, therefore logically the health will always be the default value (50), and since 50 is always less than 60 the statement will always return true. You need to pass the Ogre object in as a parameter like "Black Knight" suggested.

Edit: I'm a bit late, that's what I get for posting from my iPhone ;).
I know this sounds bad, but I'm getting ahead of what I've learned, or I'm doing stuff he wont teachm haven't figured out which one yet. But do I pass it as a parameter?

This topic is closed to new replies.

Advertisement