prevent user from inputing duplicate stings into a vector

I am trying to write a program for a game list. I have it all done but I can't get it to prevent users from entering duplicate information. here is my code:


#include <iostream>
#include <vector>
#include <iterator>
#include <string>

//tristanmross@aol.com
using namespace std;

int main ()
{
cout << "\t\t\tTHE GAME LIST\n\n";
cout << "\vWelcome to your game list!\n";

vector< string >gameInventory;

string ask;

bool correctMenu = false;

do {
cout << "\nDo you want to: find, list, add, remove, remove all, quit?\n " << endl;
cin >> ask;

if (ask == "find") {

string gameToFind;

cout << "\vInput game to find: " << endl;
cin >> gameToFind;


for (vector< string >::const_iterator iter = gameInventory.begin(); iter != gameInventory.end(); ++iter) {

if (*iter == gameToFind) {

cout << "\vThe game "<< gameToFind << " has been found!\n";
}
else {
cout << gameToFind << " is not in your inventory\n";
}

}

}

if (ask == "list") {

int itemNum = 0;

for (vector< string >::const_iterator iter = gameInventory.begin(); iter != gameInventory.end(); ++iter) {

cout << ++itemNum << ")" << *iter << endl;

}

}

if (ask == "add") {

string addGame;

cout << "What do you want to add?\n" << endl;

cin >> addGame;

vector< string >::const_iterator iter = gameInventory.begin();

bool found = false;

do {
if (*iter == addGame) {
cout << "yo";
found = true;
}
else {
gameInventory.push_back(addGame);

sort(gameInventory.begin(), gameInventory.end());

cout << "You've added " << addGame << " to your game list.\v"<< endl;
cout << "You now have " << gameInventory.size() << " in your game list.\n";
}
++iter;

} while (iter != gameInventory.end() && !found);

}



if (ask == "remove") {

string itemToRemove;

cout << "\vWhich game do you want to remove?" << endl;
cin >> itemToRemove;

bool found = false;

for (vector< string > ::iterator iter = gameInventory.begin();
iter != gameInventory.end() && !found; ++iter){
if (*iter == itemToRemove && !found) {
gameInventory.erase (iter);
found = true;
}
}
}

if (ask == "remove all") {

char removeAll;

cout << "\v\vAre you sure: (y/n)" << endl;
cin >> removeAll;


if (removeAll == 'y') {

gameInventory.clear ();

cout << "\nYour invetory is empty." << endl;

}

else {

cout << "\nOk that is fine....No erase!" << endl;

}
}

if (ask == "quit") {

cout << "\nThank you. Good bye!";

correctMenu = true;


}

} while (!correctMenu);


return 0;
}

I am having problems with the add set of code.
Last edited on
You could use an std::set instead, it prevents duplicate elements from being added.
You have not set found = true after cout << "yo".
I am kinda new to c++ could you give me a little example
i know about the found statement but when I run it it gives me an error.
Last edited on
Topic archived. No new replies allowed.