Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   Help please: Runtime error... I can't solve (http://www.go4expert.com/forums/help-runtime-error-i-solve-t20277/)

ashe_ashe 4Dec2009 18:35

Help please: Runtime error... I can't solve
 
Code:


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

I checked many times (scanning the codes and by using cout to test the output) but I cannot find the error.

Please help.

SaswatPadhi 27Dec2009 19:09

Re: Help please: Runtime error... I can't solve
 
Quote:

Originally Posted by ashe_ashe (Post 61224)
I checked many times (scanning the codes and by using cout to test the output) but I cannot find the error.

Please help.

Why then, do you think that your code has any error at all ?

Why don't u explain your problem ?
Just posting a "Help me ..." won't fetch you any help.

Rise 27Dec2009 23:03

Re: Help please: Runtime error... I can't solve
 
Do you have a image of the origional script. Double check to see if the file is correct in all catogories one wrong thing or one wrong letter will give you that message. I found myself when making scprits that somethimes on the orgional it has a sppace at the top that i dont relize and that usualy fix's it for me. But thats all the info i can give based on the info you provided. Try telling us what system your using what type of file did you put it in that stuff.

ashe_ashe 29Jan2010 21:03

Re: Help please: Runtime error... I can't solve
 
Cause it could not compile previously so I had no idea where I went wrong.

But it's solved. I didn't set my variables in the base class to abstract (well, this I tried before but I got errors... someone else managed to do it correctly for me) and forgot to create an object for the pointer *ply <--this is my biggest mistake.

But thank you for replying. I appreciate any help since I am still a beginner at this.


All times are GMT +5.5. The time now is 19:57.