How to store names using arrays

I need help with my project in which I need to create a simulated game of bowling. I specifically need help with asking how many users will be playing and then asking for their names and storing them as a c-type string/array. How can I ask for the names of the amount of players entered when I don't know how many will be playing (1-5 players)?
Create an array that can hold five names, and use only the first n elements in the array if the number of players is n.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

int main()
{
    const std::size_t MAX_PLAYERS = 5 ; // upper limit
    std::string player_names[MAX_PLAYERS] ;

    std::size_t num_players ; // actual number of players
    std::cout << "how many will be playing (1-" << MAX_PLAYERS << ")? " ;
    std::cin >> num_players ;

    if( num_players == 0 ) num_players = 1 ; // at lest one player
    else if( num_players > MAX_PLAYERS ) num_players = MAX_PLAYERS ; // at most MAX_PLAYERS

    std::cin.ignore( 10000, '\n' ) ; // throw away the new line remaining in the input buffer
    for( std::size_t i = 0 ; i < num_players ; ++i )
    {
        std::cout << "name of player #" << i+1 << ": " ;
        std::getline( std::cin, player_names[i] ) ; // a name may have more than one word
    }

    std::cout << "list of players:\n" ;
    for( std::size_t i = 0 ; i < num_players ; ++i )
        std::cout << i+1 << ". " << player_names[i] << '\n' ;

    // ...

}
Topic archived. No new replies allowed.