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.
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.
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";
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.
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.