I am working on this code and everything is working but for some reason when i go and do case 3 for remove it removes the code but continually asks for me to remmove and no matter what i type it will not go back to default options for me ot pick. i have tried break; continue; choice = 0; combination of them but nothing seems to work.

int main()
{
    vector<string> games;
    vector<int>::const_iterator iter;
    int choice = 0;
    int game;

    cout << "1. List of Games\n";
    cout << "2. Add a Game you have played\n";
    cout << "3. Remove a Game\n";
    cout << "4. Quit\n";


    while(choice != 4)
    {
        cout << "Choose an option: ";
        cin >> choice;

        switch(choice)
        {
            case 1:
            {
                for(unsigned int i = 0; i < games.size(); ++i)
                    cout << games[i] << endl;
                choice = 0;
            }
            break;

            case 2:
            {
                cout << "Enter a game to add:(nospaces)";
                string game;
                cin >> game;
                games.push_back(game);
                choice = 0;
            }
            break;

            case 3:
            {
                cout << "Your game so far:\n";

                for(unsigned int i = 0; i < games.size(); ++i)
                    cout << i << ". " << games[i] << endl;
                cout << "Enter the name of a game to remove: ";
                cin >> game;
                iter = find(games.begin(), games.end(), games);
                games.erase(games.begin() + game);
                choice = 0;
            }
            break;

            case 4:
            {
                cout << " You selected Quit.\n";
                break;
            }   

    }




    return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

You're using the beginner's guide to C++ game programming! I wrote the same program. Let's see what mistakes I can find. Below is the code I used. Let's see if we can just compare the two.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    int choice = 0;
    int location;
    string game;
    
    vector<string> games;
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;
    
    do
    {
        cout << "1: Add a new game.\n";
        cout << "2: remove a game.\n";
        cout << "3: List all games.\n";
        cout << "4: Exit.\n";
        cin >> choice;
        switch (choice)
        {
             case 1:   
                  cout << "what is the title of the game you would like to add?\n";
                  cin >> game;
                  games.push_back(game);
                  break;
             case 2:
                  cout << "what is the title of the game you would like to remove?\n";
                  cin >> game;
                  if (find (games.begin(), games.end(), game) != games.end())
                  {
                      myIterator = find(games.begin(), games.end(), game);
                      games.erase(myIterator);
                  }
                  break;
             case 3:
                  for (iter = games.begin(); iter != games.end(); iter++)
                  {
                      cout << *iter;
                  }
                  break;   
        }
    } while(choice != 4);
return 0;
}

1. You need all your #include library statements.

2. I am not sure what iter is doing in case 3. Additionally, I think that you can't use a const iterator here. I'm not positive, though.

i changed it to this and yes i am using a begginger's guide to C++ for i am in school. is there anything i did wrong in case 3 for why it is not going back to run options?

case 3:
            {
                cout << "Your game so far:\n";

                for(unsigned int i = 0; i < games.size(); ++i)
                    cout << i << ". " << games[i] << endl;
                cout << "Enter the name of a game to remove: ";
                cin >> game;
                games.erase(games.begin() + game);
                choice = 0;
            }
            break;

Edited 3 Years Ago by Dani: Formatting fixed

the break statement needs to be inside the bracket I believe. actually... never mind that. It just looks a little nicer that way.

Ehh. I think your missing a closing bracket for that while statement.

cin >> game;
games.erase(games.begin() + game);

This doesn't make any sense. You need to find the location of game, store that in an iterator, then substitute (games.begin() + game) for (iterator).

Look at what I did above.

This article has been dead for over six months. Start a new discussion instead.