class and object

This is a discussion on class and object within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I wrote a code about a die game, and it works very well, but was using twice the ...

  1. #1
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    class and object

    Hello everyone,

    I wrote a code about a die game, and it works very well, but was using twice the same kinda function, so I decide to re write the porgram using class... The code is compiled but I don't get the result I expected. Some of the variables don't keep the result they have been affected in an earlier call. I thought that I had to add staitic in front of those variables to solve the problem...unfortunately I'm using a 2-dimensional table (its dimension changes depending on how many players are playing) to store the results of every single player and game...and it seems that I can't put static in front of the table...does anyone have a solution for my problem? thanks in advance bart

    here is the code

    Code:
    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    
    using namespace std;
    
    class game
    {
        static const int LOW=1;
        static const int HIGH=6;
        int player,x,y,c,d,e,f;
        int result[][];
        public: game();
                ~game();
                void start();
                void sorte(int);
                void winner();
                
    };
            
    game::game()
    {
     cout<<"hello\n";
     cout<<"number of players: \n";
     cin>>player;  
     
     int result[player][5];
        
        for (x=1; x<=player;x++)
        {
            for(int z=0; z<=4; z++)		//though that initializing the table with 0 
            {result[x-1][z]=0;}			//will be the solution... nope :-(
        } 
    }    
    
    game::~game()
    {
    cout<<"exit\n";}
    
    void game::start()
    {
        cout<<"choose your option: \n";
        cout<<"1. play\n";
        cout<<"2. exit\n";
        cin>>f;
        srand(time(NULL));    
                
        while (f==1)
        {
        for(x=1; x<=player; x++){  
        int a,b;
        a = rand()%(HIGH-LOW+1)+LOW;
        b = rand()%(HIGH-LOW+1)+LOW;
        cout<<"x is: "<<x<<"\n";
        result[x-1][0]= x;
        cout<<"player number:"<<result[x-1][0]<<"\n";	//here I got the right results for all the players
        result[x-1][1]= a+b;
        cout<<"player result:"<< result[x-1][1]<<"\n";
        sorte(1);
        }
        
        //I'm guessing that the problem is here. The fact is that the content ofthe table result isn't transfered correctly
        //to this step of the code. I though that I needed to declare result as a static object, but it
        //doesn't let me do it...does anyone have a solution??? thanks a lot
        
        for(x=1; x<=player; x++){					//here I just get the last player result and all the previous players become the last player???
        cout<<"player "<<result[x-1][0]<< " total [" <<result[x-1][1]<< "] point ["<<result[x-1][2]<<"]\n";
        }
        
        								
    
        for(x=1; x<=player; x++){
        if (result[x-1][2]==1)
        {    
        cout<<"player "<<x<<" is a winner \n"; 
        result[x-1][3]++;   
        }    
        }    
    
        for(x=1; x<=player; x++){
        cout<<"player "<<result[x-1][0]<< " total of points[" <<result[x-1][3]<< "]\n";
        }
    
        cout<<"what do you want to do now?: \n";
        cout<<"1. play again\n";
        cout<<"2. exit\n";
        cin>>f;    
    
    }    
    }           
    
    void game::winner()
    {
         for(x=1; x<=player; x++){
        if (result[x-1][4]==1)
        {    
        cout<<"player "<<x<<" is the winner \n"; 
        }    
        }    
        
        cout<<"Thanks for playing \n";
    }    
    void game::sorte(int i)
    {
        for(x=1; x<=player; x++)
        {result[x-1][i+1]=0;}
        
        for((y=1, c=0); y<player;y++)
        {  
          if (result[c][i]<result[y][i])
            {
                for(d=0; d<y; d++)
                {result[d][i+1]=0;}
            result[y][i+1]=1;
            c=y;
            }    
            else if (result[c][i]==result[y][i])
            {
            result[y][i+1]=1;
            result[c][i+1]=1;
            }    
            else
            {
            result[c][i+1]=1;
            result[y][i+1]=0;
            }
        }
    }     
          
    int main()
    {
        game joue;
        joue.start(); 
        joue.sorte(3);   
        joue.winner();   
            
        cin.ignore();
        cin.get();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    your result array does not need to be declared as static. What it needs is to only be declared once. I would first get rid of the extra result declaration in the constructor, and see if that helps you out any.

  3. #3
    bartinla
    Join Date
    Oct 2004
    Posts
    10
    Unfortunately it doesn't change anything :-( I don't understand why I got the right result in the array in the first loop, and as soon as I go to the next step the result are messed up, and the array is filled with the last player's data?????

    thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21