0

I need to remove an element from my profile[] array, then move back all other elements in the array to fill in the now empty space. This is my attempt at doing the first part of the problem, gives a -fpermissive error, Advice?

void deleteCreature(int numCreatures, Creatures profile[])

{

for (int x = 0; x < numCreatures; x++)
{
    cout << "The following is a list of all the creatures you take care of:"
        << profile[x].name << endl << endl << endl;

    cout << "What creature do you wish to remove?" << endl
        << "CREATURE NAME: ";
    cin.ignore();
    getline(cin, profile[numCreatures].name);

    std::vector<int> array;

    auto it = std::find(array.begin(), array.end(), profile[numCreatures].name);
    if (it != array.end())
    {
        array.erase(it);
    }
    else 
    {
        std::cerr << "Could not find profile!\n";
    }

    cout << "You have removed " << profile[x].name << "." << endl << endl;*/
}

}

EDITED

8
  • 8
    Use a container insted, like std::vector or std::list. Don't re-invent the wheel. Commented Apr 23, 2014 at 15:18
  • It would help if you copy the precise error message. Commented Apr 23, 2014 at 15:19
  • 1
    @Jepessen while that is good advice in general, this kind of problem is often set as an exercise to students where this is not possible. Commented Apr 23, 2014 at 15:19
  • This is not valid C++ code. However, are you sure you don't mean profile[x] instead of profile[num] in your loop? Commented Apr 23, 2014 at 15:20
  • 2
    delete is a C++ keyword. You can't use it as the name function or variable. Choose a different name. Commented Apr 23, 2014 at 15:25

1 Answer 1

2

Why do people insist on rewriting existing code...

The standard-library does all the work for you:

Version 1: (only use if you have to use C-Style arrays)

std::remove(array,array+arraySize,profile[num].name);

Then set the last element to zero and adjust arraySize, done.

Version 2: (the C++ way to do this)

Save the contents of array in an std::vector. You can initialize it from a range, with initializer lists or push_back().

std::vector</*whatever type array stores*/> array;
//Initialize array
array.erase(std::find(array.begin(),array.end(),profile[num].name));

The vectorkeeps track of its size and allocated memory automatically, so you canot get it wrong.

If you are not sure the profile exists, test result_of_find != array.end() before you erase, in the first version check result_of_remove == arraySize - 2.

For example in version 2:

std::vector<int> array;
//Initialize array
auto it = std::find(array.begin(),array.end(),profile[num].name);
if (it != array.end()){
    array.erase(it);
}
else {
    std::cerr << "Could not find profile!\n";
    //Handle error
}
Sign up to request clarification or add additional context in comments.

8 Comments

std::vector<int> array; result_of_find != array.end(); array.erase(std::find(array.begin(), array.end(), profile[numCreatures.name])); Like this?
std::vector<int> array; Does this initialize the array?
compiler says 'vector' is not a memeber of 'std' derp derp fixed it didnt add the header
In a sense yes, but the default constructed vector is empty. You can fill it for example like std::vector<int> array = {1,2,3,4};. arraynow contains the elements 1,2,3 and 4 in that order. You need to #include <vector> to use it. For more information on vectorsee en.cppreference.com/w/cpp/container/vector
Could i populate that array with another array earlier in the program, such as where profile[numCret].name is stored?
|

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.