2021年3月20日星期六

How to get a string to add onto an array?

What I'm trying to achieve is getting my string array to output however many names the user inputs for creating the team. For example, if the user enters 5, then it should output 5 names. The issue with my code is that it is only outputting one name. It compiles and everything so no errors. The array only has 7 names and the user has to input 5 names that are supposed to be added onto the string array. Please help with it asap.

#include <iostream>  #include <cstdlib>  #include <ctime>      using namespace std;    string create_team(string people[], int size, int team_size);    int main()     {         int team_size;         srand(time(0));              string people[12] = { "Adam", "Benjamin", "Caleb", "Daniel", "Ephraim", "frank", "Gideon" };       string team = create_team(people, 12, team_size);           for (int i = 1; i <= 5; i++)        {      cout << "Enter name " << i << ": ";      cin >> team;        }               cout << "How big of a team to create? ";        cin >> team_size;          cout << "Team: " << team << endl;                 return 0;    }        string create_team(string people[], int size, int team_size)    {       string team;       bool selected[size] = { false, false, false, false, false, false, false };      /*string team = " ";        team = team + "sample";        team = team + " ";  */           int index;        do {            index = rand() % size;        }   while (selected[index] == true);            return team;    }      ```  
https://stackoverflow.com/questions/66728976/how-to-get-a-string-to-add-onto-an-array March 21, 2021 at 12:55PM

没有评论:

发表评论