0

Is there a way to make a loop that creates strings? I ask the user how many players there are, and then the loop makes that many strings in order to get all of their names... Something like this, (but not in normal english...):

cout << "How many players? " << endl;
cin >> numOfPlayers;
 
for(unsigned int i=0 ; i<numOfPlayers ; i++)
     {
     /* create new string */
     }

And then, if that's possible, how could I access each one individually?

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

you will want to create an array, or vector, of strings.

vector<string> names;
<snip>
for(unsigned int i=0 ; i<numOfPlayers ; i++)
     {
           string name;
           cout << "Enter name #" << i << "\n";
           getline(name, cin);
           names.push_back(name);
     }

>>how could I access each one individually?
From the above vector: if n is an integer between 0 and the number of names in the array

string thisName = names[n];

There is another way to access individual members of a vector - using an interator -- but its a little more complex.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.