Preventing duplication in 'Game Archiver' using iterators and vectors

Hello, I am working out of Dawson's 'Beginning C++ through game programming' and am working on an assignment which calls upon me to create a Game Archiver that keeps track of game titles for the user. The user can choose to add a title, remove a title, and quit. How can I prevent the same title from being added twice. Meaning, I do not want the user to be able to add a game title he has already added. Here is my code below, it is giving me a few errors when it comes to preventing game duplication, but everything else works okay. I am trying to set up a system where the iterator runs a find that can locate whether or not a game has been added or not. If it has been added then I want to display that the title has already been added and move on. Thanks for the help.

vector<int>::const_iterator iter;

vector<string> games(1, "- Your Games -");


cout << "1. Add Game Titles\n" ;
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";

int choice = 0;
char restart;

while(choice != 5)
{
switch(choice)
{
case 0:
{
cout << "Choose an option: ";
cin >> choice;
}
break;

case 1:
{

do

{cout << "Input game title: \n";
string gameName;
cin >> gameName;
games.push_back(gameName);
getline(cin, gameName);
choice = 0;

iter = find(games.begin(), games.end(), gameName);
if (iter != games.end())
cout << "That title already exsists.\n";


cout << "Would you like to add another title? (y/n)\n";
cin >> restart;

system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";


}while (restart == 'y' || restart == 'Y');

}
break;
Instead of std::vector, just use an std::set. It removes the duplicates for you.
Hey firedraco, I am actually unsure of how I would go about doing that. Could you please show me how? I just started learning c++ about a week ago and try to learn by example and go from there. Thanks a great deal. I will post the rest of my code in case that helps you get more of a feel of what my program is designed to do.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>


using namespace std;

int main()
{

vector<int>::const_iterator iter;

vector<string> games(1, "- Your Games -");


cout << "1. Add Game Titles\n" ;
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";

int choice = 0;
char restart;

while(choice != 5)
{
switch(choice)
{
case 0:
{
cout << "Choose an option: ";
cin >> choice;
}
break;

case 1:
{

do

{cout << "Input game title: \n";
string gameName;
cin >> gameName;
games.push_back(gameName);
getline(cin, gameName);
choice = 0;

iter = find(games.begin(), games.end(), gameName);
if (iter != games.end())
cout << "That title already exsists.\n";


cout << "Would you like to add another title? (y/n)\n";
cin >> restart;

system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";


}while (restart == 'y' || restart == 'Y');

}
break;

case 2:
{
system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";

for (unsigned int i = 0; i < games.size(); ++i)
cout << games[i] << endl;
choice = 0;

}
break;



case 3:
{
system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";

cout << "\t Titles:\n";
for (unsigned int i = 0; i < games.size(); ++i)
cout << i << ") " << games[i] << endl;
cout << "Input corresponding number of title to remove: ";
int erase;
cin >> erase;
games.erase(games.begin() + erase);
choice = 0;

}
break;

case 4:
{

for (unsigned int i = 0; i < games.size(); ++i)
games.clear();
choice = 0;

system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";
cout << "\tGame list refreshed!\n";

}
break;

default:
{
cout << "Invalid choice, please try again." << endl;
choice = 0;
}
break;
}while(choice != 5)
{
switch(choice)
{
case 0:
{
cout << "Choose an option: ";
cin >> choice;
}
break;

case 1:
{

do

{cout << "Input game title: \n";
string gameName;
cin >> gameName;
games.push_back(gameName);
getline(cin, gameName);
choice = 0;

iter = find(games.begin(), games.end(), gameName);
if (iter != games.end())
cout << "That title already exsists.\n";


cout << "Would you like to add another title? (y/n)\n";
cin >> restart;

system("cls");

cout << "1. Add Game Titles\n";
cout << "2. Game Collection\n";
cout << "3. Remove\n";
cout << "4. Remove All\n";
cout << "5. Quit\n";


}while (restart == 'y' || restart == 'Y');

}
break;
}


return 0;
}


Replace std::vector<std::string> with std::set<std::string>, and change any methods if necessary.
Info on vectors: http://www.cplusplus.com/reference/stl/vector/
Info on sets: http://www.cplusplus.com/reference/stl/set/
First off, I'd like to thank you all for the replies. I don't think i was very clear in my request. The assignment that I have to do is for class and the book we are working out of has not touched upon sets yet so aside from the fact that I don't quite understand how to implement them, the teacher actually wants us to use vectors and their corresponding iterators on this assignment. If someone could tell me why I am getting an error when running this specific code it would do wonders. This is a great site, thanks in advance to anyone who can shed some light.

{cout << "Input game title: \n";
string gameName;
cin >> gameName;
games.push_back(gameName);
getline(cin, gameName);
choice = 0;

iter = find(games.begin(), games.end(), gameName);
if (iter != games.end())
cout << "That title already exsists.\n";
1) What is the error?
2) I would put the check around the entire "add game" portion of the code
3) Please use code tags:
http://www.cplusplus.com/articles/firedraco1/
Looking at the code I see 3 problems:

1: You have your inerator defined wrong: You have

vector<int>::const_iterator iter;

should be

vector<string>::const_iterator iter;

2: Your getting a game title from the user with cin >> gameName your then calling getline(cin, gameName) which is replacing gameName with an empty string, remove this line.

3: You are adding to the vector first and then checking if it is in the vector, this is backwards, it will think every game title already exists.

Revised code:

1
2
3
4
5
6
7
8
9
10
cout << "Input game title: \n";
string gameName;
cin >> gameName;

iter = find(games.begin(), games.end(), gameName);

if (iter != games.end())
  cout << "That title already exsists.\n";
else
  games.push_back(gameName);
Thanks for that tip binarybob! It seems like i'm heading in the right direction now! However, I am still getting an error that says

error: error: no matching function for call to 'find(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::string&)'

The string that I established is suppose to collect game titles from the user and input it into a new vector. The vector string should not be already established prior to the game starting. Thanks for any further input.
Thanks for all the support and help everyone, I finally got it to run smoothly. I forgot to have the #include <algorithm> at the top of my code. DOH!

Such a newb mistake, hehe.

btw, this site is fantastic.
Topic archived. No new replies allowed.