Program using Vectors and Iterators

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.

Here's my "attempt" so far:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Game List
//Enables you to list, remove and add games

#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

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


Last edited on
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.
Thank you for the quick response, I'll give it another go and show you the results. Would a switch work better for something like this?

Could I also put everything in separate braces under one if statement?
Last edited on
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.

if( condition ) {
statement-block;
}


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.
Topic archived. No new replies allowed.