3

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:

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;
4
  • You've got some extra commas in your teams array. Also, as mentioned below, using [] doesn't make the array variable-length; it makes its length depend on the length of the initializer list, in this case 4. From that point on, it's the same as a fixed-length array. std::vector is what you really want. Commented Oct 21, 2012 at 23:33
  • The comments are on purpose. I am learning arrays and told only to use arrays... Commented Oct 21, 2012 at 23:42
  • I said commas not comments. E.g. "St. Louis," should be "St. Louis". Commented Oct 21, 2012 at 23:42
  • I'm confused. Arrays are fixed length. Thus "add it to the array" makes no sense; no matter what length you pick, at some point you can run out of slots. Now if you are supposed to use arrays on the heap, then you want the second paragraph of Dietmar's answer. Commented Oct 21, 2012 at 23:46

3 Answers 3

4

The size of built-in arrays is immutable: You can neither remove elements not can you add any. I would recommend using a std::vector<std::string>, instead: Adding elements to a std::vector<T> can, e.g., be done using push_back(). To remove an element you would locate an element, e.g., using std::find(), and then use erase() to remove it.

If you need to use built-in arrays (although I don't see any good reason for this), you'd allocate an array on the heap using new std::string[size] and maintain its size, appropriately releasing memory at opportune times using delete[] array;.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Vectors are definitely the way to go if you want things to be dynamic, anywhere.
0

Using an array, you could treat empty array cells with a char* such as "EMPTY". To find an item you search through the array , and find to "replace" or add it.

const char * Empty = "EMPTY";
cout << "Please enter a city you want to add:"
cin >> city;
for(int i = 0; i < Arr_Size; i++) //variable to represent size of array
{
    if(Arr[i] == Empty) //check for any empty cells you want to add
    {
       //replace cell
    }
    else if(i == Arr_Size-1) //if on last loop
       cout << "Could not find empty cell, sorry!";
}

As for removing a cell:

cout << "Please enter the name of the city you would like to remove: ";
cin >> CityRemove;

for(int i = 0; i < Arr_Size; i++)
{
    if(Arr[i] == CityRemove)
    {
        Arr[i] = Empty;             //previous constant to represent your "empty" cell
    }
    else if(i == Arr_Size - 1)    //on last loop, tell the user you could not find it.
    {
        cout << "Could not find the city to remove, sorry!";
    }
}

Printing the array while skipping the 'empty' cells //Printing the array

for(int i = 0; i < Arr_Size; i++)
{
    if(Arr[i] != Empty)             //if the cell isnt 'empty'
    {
        cout << Arr[i] << endl;
    }
}

But i do agree using a vector would be a much more efficient approach, this is simply a creative approach to get your mind thinking.

4 Comments

Why not use a zero-length string, i.e. ""?
edited the post to show my intent, when printing it skips the 'empty' cells.
It looks to me like you've declared Arr as std::vector<std::string> Arr;... you should show that. string Arr[] would not work because there's no size method for built-in arrays.
Im using Arr to represent an array, and size a function to show the size of the array, ill edit the loops. I usually make size functions for arrays in my programs.
0

To add information to an array, you could do something like this:

for (int i = 0; i < 10; i++)
{
    std::cout << "Please enter the city's name: " << std::endl;
    std::getline(cin, myArray[i]);
}

I'm not sure what you mean by removing something from an array. Do you want to set the element's value to 0, which would result in something like {"City 1", "City 2", 0, "City 3}. Or do you want to remove it from the array and move the other elements to fill its space, which would result in something like {"City 1", "City 2", "City 3"}?

1 Comment

It's an array of string, not const char*, so I'm not sure that having nulls in the initializer list will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.