Game list_Immediate Help

I'm trying to add a loop which checks if there is any duplicate names. I can't come up wit an answer. I know the concept, but I can't apply it. Would you help me figure it out?


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

using namespace std;

int main()
{
bool quit = false;
int choice;
char game;
string inventory;

vector<string>::iterator iter;
vector<string> gameTitles;

while (!false)
{
cout << "\t\t\t My Favorite Games List\n\n";
cout << "Choose from one of the following options: " << endl;
cout << " 1. Add title \n 2. Delete title \n 3. Clear list \n\n";
cout << "Type in any numbers to start! " << endl;

if(!gameTitles.empty())
{
for (iter = gameTitles.begin(); iter != gameTitles.end(); ++iter)
{
cout << "-" << *iter << endl;
}
}

cout << " ";
cin >> choice;

if (cin.fail())
{
cin.clear();
cin.ignore();
}

if (choice == 1)
{
cout << "Type in the name of the game you would like to add:\n";
cin >> inventory;
gameTitles.push_back(inventory);
}
else if (choice == 2)
{
cout << "Type in the name of the game you would like to remove:\n";
cin >> inventory;

for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter)
{
if(*iter == inventory)
{
cout << "erased";
iter = gameTitles.erase(iter);
}
else
{
++iter;
}
}
}

else if(choice == 3)
{

cout << "Are you sure? (y/n) ";
cin >> game;
if(game == 'y')
{
gameTitles.clear();
}
}
else
{
cout << "\nInvalid Choice, Please try again.\n";
}
}
}
closed account (3qX21hU5)
First off please use codetag whenever you post your code to the forums (Highlight all your code and then press the <> off under Format:)

Now the easiest way to do this would be to add it to this.

1
2
3
4
5
6
if (choice == 1)
{
cout << "Type in the name of the game you would like to add:\n";
cin >> inventory;
gameTitles.push_back(inventory);
}


Which lets the user enter a new name of a game. Since this is the only time the user could add a name that would be a duplicate.

All you need to do is right after they enter the name of the title you add the loop that checks to see if that name matches any name in your vector, if it doesn't add that new name into the vector.

Here is a function that would do it.

1
2
3
4
5
6
7
8
9
10
11
12
bool sameTitle(vector<string> games, string newGame)
{
    bool same = false;
    
    for (vector<string>::size_type i = 0; i != games.size(); ++i)
    {
        if (newGame == games[i])
            same = true;
    }
    
    return same;
}


Here is how it works. You enter the vector that stores all the titles and the new title you wish to enter as arguements. It then scans the vector to see if the new title you wish to enter is the same as any other title in the vector. It is the same the function will return true and if it is not it will return false.

So you could implement it into your program like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    if (choice == 1)
    {
        cout << "Type in the name of the game you would like to add:\n";
        cin >> inventory;
        if (sameTitle(gameTitles, inventory))
        {
            cout << "The game title you just entered is already in the vector. \n" << 
                    "Now returning to the main menu! \n" << endl;

            continue; // Returns to the beginning of your while loop
        }
        else
        {
            gameTitles.push_back(inventory);
        }
    }


Last edited on
Topic archived. No new replies allowed.