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