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;
}

Dani AI

Generated

Short diagnosis: the remove case is mixing up variable types and using the wrong object in the call to std::find, which leaves the program stuck in the same branch or in a failed-input state. Typical problems in the code above are: an int named game at file scope while a string named game is created inside another block (shadowing), using vector<int>::const_iterator instead of a string iterator, calling find(..., games) instead of find(..., game), and doing games.begin() + game when game is not a valid index. 's suggestion to include the right headers and to use an iterator for erase is correct; the rest are implementation details that need fixing.

Concrete fixes to apply

  • Use a single, clearly typed variable for the item you read: either read a numeric index into an unsigned/size_t, or read a game title into a std::string. Do not shadow variables.
  • Include <algorithm> for std::find, plus <vector>, <string>, <iostream>.
  • If removing by name, call auto it = std::find(games.begin(), games.end(), title); and check it != games.end() before erasing.
  • If removing by index, validate 0 <= index && index < games.size() before games.erase(games.begin() + index).
  • Always validate user input. If cin fails (user typed letters when you expected a number), call cin.clear() and cin.ignore(...) to recover.

Example (remove-by-name flow)

// assumes std::vector<std::string> games; and proper headers
if (games.empty()) { cout << "No games.\n"; break; }
cout << "Enter full title to remove: ";
std::string title;
std::getline(cin, title);
if (title.empty()) std::getline(cin, title); // handle leftover newline

auto it = std::find(games.begin(), games.end(), title);
if (it != games.end()) games.erase(it);
else cout << "Title not found.\n";

Quick debug checklist: print the value/type of the variable you read, verify cin.fail() after input, verify it != games.end() before erase, and avoid setting choice inside cases just to force loop behavior — handle the loop condition cleanly. This will resolve the repeated-remove prompt and make the menu flow predictable.

Recommended Answers

All 4 Replies

Why did you ignore ALL the places which tell you about code tags?

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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.