Please bear with me as I'm quite new to all this, I'm busy reading through Beginning Game Programming with C++ by Michael Dawson. I've been instructed to "Write a program using vectors and iterators that allows the user to maintain a list of games his or her favourite games. The program should allow the user to list all game titles, add a game title and remove a game title".
I'm particularly confused about how exactly remove a game title after getting input from the user.
//Game List
//Enables you to list, remove and add games
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
usingnamespace std;
int main()
{ vector<string>::const_iterator display;
vector<string> games;
vector<string>::iterator delchoice;
games.push_back("Conan");
games.push_back("MK vs DC");
games.push_back("Burnout Paradise");
cout << "\t\tWelcome to your Game List\n\n";
cout << "Here are the available commands:\n\n";
cout << "1. List: Lists all your titles\n";
cout << "2. Add: Adds game of your choice\n";
int choice;
cin >> choice;
if (choice = 1)
for (display = games.begin(); display != games.end(); ++display)
cout << *display << endl;
cin >> choice;
if (choice = 2)
cout << "Type in the name of the game you would like to add:\n";
string game;
cin >> game;
games.push_back(game);
cout << "You added: " << game;
cin >> choice;
if (choice = 3)
cout << "Type in the name of the game you would like to remove:\n";
cin >> game;
*delchoice = game;
{if (*delchoice = games(game))
games.erase(*delchoice)}
cin >> choice;
system("PAUSE");
return 0;
}
First, all your if() statements are assignments, not comparisons.
Second, you need braces, lots of 'em, around your if() statements.
std::find( games.begin(), games.end(), game )
returns an iterator to the element in the vector matching the name of the game entered by the user, or games.end() if not found. vector<>::erase( Iterator i ) erases an element referred to by the given iterator.
whether you use a switch or an if-else if-else if-else is up to taste. It does not matter to the compiler, and quite frankly although the compiler may generate slightly different code in both cases, you won't see an appreciable performance difference unless you start executing the code millions of times.
I just made revisions on the program, works like a charm, thanks again for your assistance "jsmith". It's MUCH appreciated. Quite hard studying for yourself when you don't have a lecturer or some other senior figure to answer questions.