#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.

First of all, replace all your getch() with cin.get().

Your class animal_game has 2 members function, but they're not implemented, so this wont compile because if you say you'll return a value from a function you need to return a value from a function :)
Change your class to

class animal_game{
    public:
            virtual string name() = 0;
            virtual int leg () = 0;
};

But your main problem is in main(). You say:

int main ()
{
    play *ply;
[.......]
          ply->main_game_play();}
}

How can you access " main_game_play" when ply is just a dangling pointer? answer: you can't. You need to assign a new object to "ply" with new and delete this object afterwards:

int main ()
{
    play *ply = new play();
    [....]
    ply->main_game_play();}
    [.....]
    delete ply;
    return 0;
}

Edited 7 Years Ago by Nick Evan: n/a

Thank you very much for your help and explanation. It was very clear and easy to comprehend for a newbie like me :)

This article has been dead for over six months. Start a new discussion instead.