Trying to erase elements within a vector

The program asks for the users favorite games, then to add, remove, or finish the list. I can't figure out how to remove a game within the vector. I've tried the erase() member function and the find() function. This is the main loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

    vector<string> favGames;
    string game, action;
    vector<string>::iterator it1;
    vector<string>::const_iterator iter;

  do
    {
        cout << "Here is your list of games:\n";
        for (iter = favGames.begin(); iter != favGames.end(); ++iter)
            cout << *iter << endl;

        cout << "\nWhat would you like to do? (add, remove, finish)\n";
        cin >> action;

       if (action == "add") {
        cout << "What game would you like to add?:  "; // add a game
        cin >> game;
        favGames.push_back(game);
       }
       else if (action == "remove"){
        cout << "What game would you like to remove?\n"; // remove a game
        cin >> game;

        it1 = find(favGames.begin(), favGames.end(), game); //HERE'S MY PROBLEM
        favGames.erase(*it1);
        }

        else if (action != "add" || action != "remove" || action != "finish")
            cout << "That is not an option.";
    } while (action != "finish");

// Critique on anything else you notice is welcomed
Last edited on
http://www.cplusplus.com/reference/algorithm/find/

1
2
3
4
5
6
7
8
9
// using std::find with vector and iterator:
  std::vector<int> myvector {4,6,8,50,100};
  std::vector<int>::iterator it;

  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end()) // this is required.
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myints\n";


line 26 favGames.erase(it1); not *it1
Last edited on
Topic archived. No new replies allowed.