Your logic is wrong
- when you copy elements from inventory to inventory2, except the one the user picked.
- when you print the final result as you print the old inventory, not the new inventory2.
To fix #1, I'd do something like this:
for(int i = 0; i < inventory.size(); i++) {
if (answer != inventory[i]) {
inventory2.push_back(inventory[i]);
}
}
inventory.clear();
By the way, your whole code is overcomplicated. You should make use of the C++ standard library functions.
Here's how I'd rewrite it:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> inventory = { "map", "pc", "atlas", "compass" };
void PrintContent(const std::string & message)
{
std::cout << message;
for (size_t i = 0; i < inventory.size(); i++)
{
if (i > 0)
std::cout << ',';
std::cout << ' ' << inventory[i];
}
std::cout << std::endl;
}
int main() {
// Welcome
std::cout << "Welcome to my inventory." << std::endl;
PrintContent("In the inventory, there is:");
// Get the item to be removed.
std::vector<std::string>::iterator itemPosition;
std::string itemToBeRemoved;
do
{
std::cout << "What would you like to remove? ";
getline(std::cin >> std::ws, itemToBeRemoved);
itemPosition = std::find(inventory.begin(), inventory.end(), itemToBeRemoved);
} while (itemPosition == inventory.end());
// Removing the item
std::cout << "Removing " << itemToBeRemoved << " from inventory..." << std::endl;
inventory.erase(itemPosition);
PrintContent("The new content of the inventory is:");
return 0;
}