Consolidate multiple input/output statements

Hi. The other day, I decided to write a small application for my favorite game just to see if I could. So the basic idea is that you input 5 player's names, and the application will randomly assign them a character to play.

Anyway, here's my question. So far, when asking for the player's name, here is what I have (I store each player's name in it's own variable):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout<< "Input the name of Player 1: \n";
cin>> Player1;
endl (cout);
		
cout<< "Input the name of Player 2: \n";
cin>> Player2;
endl (cout);
		
cout<< "Input the name of Player 3: \n";
cin>> Player3;
endl (cout);
		
cout<< "Input the name of Player 4: \n";
cin>> Player4;
endl (cout);

cout<< "Input the name of Player 5: \n";
cin>> Player5;
endl (cout);


I was wondering if there is any way to consolidate that down to maybe one or two statements, rather than having to write it out 5 times? I thought about using a for loops to do it, but I wasn't sure if it was even possible.
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

std::string get_player_name( int n )
{
    std::cout << "Input the name of Player " << n << ": " ;
    std::string name ;
    std::getline( std::cin, name ) ;
    return name ;
}

int main()
{
    enum { NPLAYERS = 5 } ;
    std::string player_names[NPLAYERS] ;
    for( int i = 0 ; i < NPLAYERS ; ++i ) player_names[i] = get_player_name(i+1) ;
    // ...
}
Would it not be possible to apply the names to their separate variables as well? I stored each name in it's own variable because later I need to apply a random number to each one so that each player can be assigned a random character to play from an array. If you need to see what I have in order to help out, just let me know.
> Would it not be possible to apply the names to their separate variables as well?

Yes, of course. With something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
inline void get_player_name( std::string& name, int n )
{
    std::cout << "Input the name of Player " << n << ": " ;
    std::getline( std::cin, name ) ;
}

int main()
{
    std::string player_one, player_two /* , ... */ ;
    get_player_name( player_one, 1 ) ;
    get_player_name( player_two, 2 ) ;
    // etc...
}


Another option would be to use an array as before, and then create aliases for the strings in the array.
1
2
3
4
5
enum { NPLAYERS = 5 } ;
std::string player_names[NPLAYERS] ;
std::string& player_one = player_names[0] ;
std::string& player_two = player_names[1] ;
// etc 



> later I need to apply a random number to each one so that
> each player can be assigneda random character to play from an array

Perhaps the best option would be:
1
2
3
4
5
6
7
8
9
enum { NPLAYERS = 5, NROLES = 5 } ;
static_assert( NPLAYERS <= NROLES, "not enough roles." ) ;
std::string player_names[NPLAYERS] ;
// read in player names

std::string roles[NROLES] ;
std::srand( std::time(nullptr) ) ;
std::random_shuffle( std::begin(roles), std::end(roles) ) ;
// and now player_name[0] has role[0], player_name[1] has role[1] and so on 

Topic archived. No new replies allowed.