Hello everyone,
I need to write code such that:
.you can select number of players(2,3or4) and put that number in array..
.display message if correct number not entered.
.then each player must be able to choose their color(red, blue, yellow,green)
.two players cannot be assigned the same colour
This is what i have done.. Can someone tell me if it is the right way to do this? its a bit length...

int num_players,p1color,p2color,p3color,p4color;
char player_color;
int players[1];
bool selectred,selectblue,selectyellow,selectgreen;
cout<<"Select number of players: ";
cin>>num_players;
players[0]=num_players; 

 if((num_players>=2) && (num_players<=4))
    { switch(num_players)
        { case 2:
          cout<<"Player 1, Choose your house(R for Red,B for Blue,Y for Yellow,";
          cout<<"G for Green" <<endl;
          cin>>p1color;
          if(p1color=='R' || 'r')
             { cout<<"Player 1 is initialised Red";
               selectred=true;
               selectblue=false;
               selectyellow=false;
               selectgreen=false;
             }
          else if(p1color=='B' || 'b')
             { cout<<"Player 1 initialises Blue" ;
               selectred=false;
               selectblue=true;
               selectyellow=false;
               selectgreen=false;
             }
           else if(p1color=='Y' || 'y')
             { cout<<"Player 1 initialises Yellow" ;
               selectred=false;
               selectblue=false;
               selectyellow=true;
               selectgreen=false;
             }
           else if(p1color=='B' || 'b')
             { cout<<"Player 1 initialises Green" ;
               selectred=false;
               selectblue=false;
               selectyellow=false;
               selectgreen=true;
             }
                
        }  
    }      
       //case 2..
    
    cout<<"Error!Please enter a valid number of players(2,3 or 4)" <<endl;
    cout<<"Select number of players: ";
    cin>>num_players;
    players[0]=num_players;

>>int players[1];
Why is that an array? might as well just declare it as a simple integer instead of an array of 1 element.

>> if(p1color=='R' || 'r')
That's incorrect. Here's the correction: if(p1color=='R' || plcolor=='r')

i have corrected my code..dont you think it will be too lengthy if i do it this way? is there another way to do that...?
I have to write code for 2 players,same for 3 and 4 players..

>>dont you think it will be too lengthy if i do it this way
Yes I do.

>>is there another way to do that...?
Probably several ways. I would (1) define some color numbers

#define BLUE 1
#define GREEN 2
#define RED 3
#define YELLOW 4
// etc for all supported colors

Than set an int color to be one of the above numbers. You could also use enumeration if you know how to use them yet.

Create a structure that contains all the information needed for one user

struct person
{
    std::string name;
    int color;
    // other information here
}

Pass an instance of the above structure to a function whose job is to fill in all the information in the structure. You only write the code once and only in one place -- no need to repeat the same code for every user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.