#include <iostream>
#include <string>
#include <cmath>
#include <conio.h>
#include <cstdlib>
using namespace std;
class animal_game{
public:
string name(){};
int leg (){};
};
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class bird : public animal_game{
public:
string name ()
{return (" Bird ");}
int leg ()
{return (2);}
};
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class fish : public animal_game{
public:
string name ()
{return (" Fish ");}
int leg ()
{return (0);}
};
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class insect:public animal_game{
public:
string name ()
{return (" Insect ");}
int leg ()
{return (6);}
};
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class play {
private:
int pl1,pl2,no,luck;
public:
animal_game *ani;
play ()
{ srand (time(NULL));
pl1=0;
pl2=0;
}
void find_luck()
{
luck=rand()%10;
}
void find (){
no=rand()%3;
if (no==0)
ani=new fish();
else if (no==1)
ani=new bird();
else
ani=new insect();
}
int score()
{return(luck * ani->leg());}
void main_game_play()
{
int hlp1,hlp2;
cout << endl <<" Player 1: Press ENTER to obtain an animal."<< endl ;
find_luck();
find();
hlp1=score();
getch();
cout << endl << " Player1 obtained 1 " << ani->name()
<< " with lucky number " << luck << ". Total legs = "
<< score() << endl;
pl1 = score()+pl1;
cout << endl <<" Player 2: Press ENTER to obtain an animal." <<endl;
find_luck();
find();
hlp2=score();
getch();
cout << endl << " Player 2 obtained 1 " << ani->name()
<< " with lucky number " << luck << ". Total legs = "
<< score() << endl;
pl2 = score()+pl2;
if (hlp1 > hlp2)
{
cout << endl <<" Player 1 WIN!" << endl;
cout << " Total score (leg count): " <<endl;
cout << " Player 1 Score is: " << pl1 << endl
<< " Player 2 Score is: " << pl2 << endl;
}
else if (hlp1<hlp2)
{
cout << endl <<" Player 2 WIN!" << endl;
cout << " Total score (leg count): " <<endl;
cout << " Player 1 Score is: " << pl1 << endl
<< " Player 2 Score is: " << pl2 << endl;
}
else if (hlp1 == hlp2)
{
cout << endl << " DRAW!!!" << endl;
cout << " Total score (leg count): " <<endl;
cout << " Player 1 Score is: " << pl1 << endl
<< " Player 2 Score is: " << pl2 << endl;
}
}
~play(){
if (pl1 == pl2)
cout << "Draw!\n";
else if (pl1 > pl2)
cout << "Winner: Player1. Congrats!\n";
else
cout << "Winner: Player2. Congrats!\n";
cout << endl;
delete ani;
ani = NULL;
system("pause");
}
};
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
int main ()
{
play *ply;
int r,i;
char ans;
do{
cout << " How many rounds you want to play -> ";
cin >> r;
for (i=1;i<=r;i++)
{cout << "Round #" << i << endl;
ply->main_game_play();}
cout << " Do you want to continue (Please enter Y (Yes) or N (No)) -> ";
cin >> ans;
}while(ans== 'Y' || ans == 'y');
return 0;
}
Ugh, the deadline is today and I can't solve the problem. The program operates half-way then terminates itself. It's a program where you have to pick how many rounds you want to play then the program will pick a random number from the randomizer.
Please help me find the error.