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)?
#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
elseif( 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' ;
// ...
}