#include<iostream>
#include<string>
using namespace std;
class enemy
{
private:
int m_health; //the health of each enemy
string m_name; //name of enemy jumping you
enemy* pNext; //points to next object on the list
public:
void taunt(); //taunts the user
void setNext(enemy* next); //gives next enemy on the list
enemy* getNext() const; //returns next enemy on the battlefield
string getName(); //returns the enemies name
enemy(string name = " ", int health = 100);
};
enemy::enemy(string name, int health):
m_health(health),
m_name(name),
pNext(0)
{}
void enemy::setNext(enemy* next)
{
pNext = next;
}
enemy* enemy::getNext() const
{
return pNext;
}
void enemy::taunt()
{
cout << "Ha Ha You suck, you dumb loser !" << endl;
}
string enemy::getName()
{
return m_name;
}
class battlefield
{
private:
enemy* m_pHead; // the top of the chain
public:
void AddEnemy();
void DeleteEnemy();
void EnemyTaunt();
void Clear();
void GetEnemiesName(); //list through all the names of the enemies made
void searchName(string* enemyName);
void killEnemy(); //decides which enemy to kill
battlefield();
~battlefield();
};
battlefield::battlefield():
m_pHead(0)
{}
battlefield::~battlefield()
{
cout << "Clearing the BATTLEFIELD !" << endl;
Clear();
}
void battlefield::Clear()
{
while(m_pHead != 0)
{
DeleteEnemy();
}
}
void battlefield::AddEnemy()
{
cout << "What type of enemy is this: ";
string enemyType;
cin >> enemyType;
enemy* pNewEnemy = new enemy(enemyType);
if(m_pHead == 0)
{
m_pHead = pNewEnemy;
}
else
{
enemy* pIter = m_pHead;
while(pIter->getNext() != 0)
{
pIter = pIter->getNext();
}
pIter->setNext(pNewEnemy);
}
}
void battlefield::DeleteEnemy()
{
if(m_pHead == 0)
{
cout << "The garage is empty !" << endl;
}
else
{
enemy* pTemp = m_pHead;
m_pHead = m_pHead->getNext();
delete pTemp;
}
}
void battlefield::GetEnemiesName()
{
enemy* pFirstEnemy = m_pHead;
cout << pFirstEnemy->getName() << endl;
while(pFirstEnemy->getNext() != 0)
{
pFirstEnemy = pFirstEnemy->getNext(); // gets the next object
cout << pFirstEnemy->getName() << endl; // returns name
}
}
void battlefield::searchName(string* enemyName)
{
enemy* searchEnemy = m_pHead;
string p_EnemyName;
if( searchEnemy->getName() == *enemyName)
{
cout << "We've found the enemy, He's FIRST !" << endl;
}
//iterating through list of objects
while(searchEnemy->getNext() != 0)
{
searchEnemy = searchEnemy->getNext(); //gets next pointer
p_EnemyName = searchEnemy->getName(); // gets the name
if( *enemyName == p_EnemyName )
{
cout << "We found the enemy !" << endl;
}
}
}
void battlefield::killEnemy()
{
cout << "Type in the name of the enemy you would like to kill: ";
string enemyName;
cin >> enemyName;
enemy* p_EnemyKill = m_pHead;
enemy* pEnemyName;
while(p_EnemyKill->getNext() != 0)
{
p_EnemyKill = p_EnemyKill->getNext();
*pEnemyName = p_EnemyKill->getName();
}
delete pEnemyName;
pEnemyName = 0;
}
void battlefield::EnemyTaunt()
{
enemy* tauntEnemy = m_pHead; // the head enemy will taunt.
cout << "The enemy is taunting you.\n";
tauntEnemy->taunt();
}
int main()
{
battlefield theBattleField;
cout << "Enemies are spawning !" << endl;
int number;
cout << "Please enter a number: ";
cin >> number;
for(int i = 0; i < number; i += 5)
{
theBattleField.AddEnemy();
}
theBattleField.EnemyTaunt();
theBattleField.GetEnemiesName();
cout << "Type in the name of the enemy you would like to search: ";
string enemySearch;
cin >> enemySearch;
theBattleField.searchName(&enemySearch);
theBattleField.killEnemy();
theBattleField.GetEnemiesName();
return 0;
}
I made this game, where the user enters a number and makes a bunch of enemies based on that number. What I wanted to try was calling a function that asks a user to type in the enemy name, and will kill that enemy. everytime i run the game crashes. Any Ideas on why ?