0

I am trying to code a list of strings. The user can add to the list or remove from the list as well as display the current list.

The displaying list and adding to list is working fine, but I can't figure out how to remove the user's string by iterating through the list to find a match.

How would I change my code to fix this? Look at if(answer == 3)

// InClassAssignment-FavouriteGameList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    vector <string>  gameList;
    int answer = 0;
    bool cont = true;
    int size = 0;
    vector<string>::iterator iter;
    string addToList;
    string removeFromList;

    cout << "\tGame List" << endl;

    while (cont)
    {
        cout << "--------------------------------------------" << endl;
        cout << "\nWhat do you want to do?";
        cout << "\n1 - Display List";
        cout << "\n2 - Add to List";
        cout << "\n3 - Remove from List";
        cout << "\n4 - End program" << endl << "Selection: ";
        cin >> answer;

        cout << endl;

        if (answer == 1)
        {
            cout << "List: ";
            for (iter = gameList.begin(); iter != gameList.end(); ++iter)
            {
                if (iter != gameList.end() - 1)
                    cout << *iter << ", ";
                else
                    cout << *iter << endl;
            }
        }

        else if (answer == 2)
        {
            cout << "Type in a game to add: ";
            cin >> addToList;
            gameList.push_back(addToList);
            cout << "\nAdded (" << addToList << ") to your list." << endl;
        }

        else if (answer == 3)
        {
            //display list
            cout << "List: ";
            for (iter = gameList.begin(); iter != gameList.end(); ++iter)
            {
                if (iter != gameList.end() - 1)
                    cout << *iter << ", ";
                else
                    cout << *iter << "\n" << endl;
            }

            //ask which one to remove
            cout << "Which game should be removed?: ";
            cin >> removeFromList;

            //loop/iterate through the list to find a match and erase it
            for (iter = gameList.begin(); iter != gameList.end(); ++iter)
            {
                if ()
                    cout << "\nRemoved (" << removeFromList << ")" << endl;
                else
                    cout << "\nGame not found" << endl;
            }

        }

        else
        {
            cont = false;
        }

    }

    cout << "\nThanks for using the program!\n" << endl;

    return 0;
}
Will
  • 21
  • 4

2 Answers2

5

You can use std::find to get the iterator matching the item you want to remove, then call vector::erase(iter)

auto iter = std::find(gameList.begin(), gameList.end(), removeFromList);
if (iter != gameList.end())
{
    gameList.erase(iter);
}
w.b
  • 11,026
  • 5
  • 30
  • 49
  • Note: You do not need the if. erase is valid for the range [begin(), end()] –  Feb 11 '15 at 20:31
  • it worked thanks, only thing im confused about it why use auto instead of not writing it at all? – Will Feb 11 '15 at 20:35
  • 3
    @DieterLücking Actually, for the single argument form of `erase` the check is necessary because the argument is required to be dereferenceable, and `end()` isn't. It's mentioned somewhere in the table for sequence container requirements. The requirement is [listed on cppreference](http://en.cppreference.com/w/cpp/container/vector/erase) also. – Praetorian Feb 11 '15 at 20:39
  • Thanks @Praetorian: Looking at 23.2.3 Sequence containers makes my note wrong –  Feb 11 '15 at 20:51
1

Take a look at remove. It moves all matching elements at the end of the sequence, you may then truncate it:

input.erase(
    remove(input.begin(), input.end(), s)
  , input.end());

Edit: incorporated suggestion from Fred's comment.

Adam Sosnowski
  • 1,126
  • 9
  • 7
  • 1
    I'd prefer the [erase-remove](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom) idiom. See http://stackoverflow.com/q/22860119/10077 – Fred Larson Feb 11 '15 at 20:59