While I'm aware that this is just a simple thing to do, I've tried to make this into a function. While mine kind of works, I'm more interested in how other people would go about it.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
bool restart = true;
std::string choice;
bool inInventory(std::vector<std::string> vec, std::string inItem) {
std::vector<std::string>::iterator invent;
invent = std::find(vec.begin(), vec.end(), inItem);
if (invent != vec.end()) {
return true;
} else {
return false;
}
}
void dropItem(std::vector<std::string> vec, std::string itemToDrop) {
std::vector<std::string>::iterator invent;
invent = std::find(vec.begin(), vec.end(), itemToDrop);
if (inInventory(vec, itemToDrop)) {
std::remove(vec.begin(), vec.end(), itemToDrop);
} else {
std::cout << "Item is not in vector!\n";
restart = true;
}
}
int main() {
std::vector<std::string> shoppingList;
shoppingList.push_back("Butter");
shoppingList.push_back("Cheese");
shoppingList.push_back("Candy");
while (restart) {
restart = false;
for (auto e: shoppingList) {
std::cout << e << ", ";
}
std::cout << "Pick an item to drop\n>";
std::cin >> choice;
dropItem(shoppingList, choice);
for (auto e: shoppingList) {
std::cout << e << ", ";
}
return 0;
}
}
Here is the code in case anyone is interested.
BTW: I'm not requesting anyone to fix this. I'm just wondering how the process would go for those more skilled than me.
Thanks!
What I have tried:
Messing around with functions and vectors, mainly the
std::remove();
function.