Adding or Removing Element to Array

Say I get user input. If what they type in is not already in the array(how do I check an array?), add it to the array. And vice versa how do I remove something from an array given the user input.

Example:

1
2
3
4
5
6
7
string teams[] = {"St. Louis,","Dallas","Chicago,","Atlanta,"};

cout <<"What is the name of the city you want to add?" << endl;
    cin >> add_city;

 cout <<"What is the name of the city you want to remove?" << endl;
    cin >> remove_city;
In the above case, its an array of 4 elements teams[4]. As the array size is fixed, it may not be possible to remove an item from the array.
Rather, you can assign null or someother value to the corresponding element.

Suppose user wants to add city 'texas' and he wants to remove 'dallas', then in this case
1
2
3
4
5
6
7
8
9
10
for(int i=0;i<4;i++)
{
   if(teams[i]=="Dallas")
   {
         teams[i]=="Texas";
         break;
    }
}



Other than this, string function such as string find,string compare are available. you may make use of those
Last edited on
In this case, why not use an std::list<string> instead of an array?
Topic archived. No new replies allowed.