Minor logical issue

Hello.

I'm new to programming and have been learning using the book "Beginning C++ Through Game Programming, Third Edition" by Michael Dawson to learn the ropes. I'm currently working on the exercise:

"Write a program using vectors and iterators that allows a user to maintain
a list of his or her favorite games. The program should allow the
user to list all game titles, add a game title, and remove a game title.
"

The problem I have is through line 29 to 43 (case ADD, inside my switch (choice) ) where I must add names to my games vector. Everything is fine until you leave the case ADD, then the information is lost (thus preventing me from showing anything in my case LIST). I'd like some help in figuring out why this is happening, and what I could do to prevent it.

*Note : case REMOVE doesn't return anything at the moment, at least not until I figure out why the information is lost in case ADD.

*Note 2: Some of the methods used may seem counterproductive (like the use of enum), but I'm trying out various codes from previous chapters of the book as well so I don't forget about them. Although if you have any tips, I'd really appreciate it.

Here's the code :
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    start:
    cout << "\t\t\tList of Games\n\n";

    enum choice {ADD = 1, REMOVE = 2, LIST = 3, CLEAR = 4, END = 5};
    vector<string> games;
    vector<string>::const_iterator iter;
    int choice;

    cout << "Choose an action.\n";
    cout << "1. ADD to add to your list.\n";
    cout << "2. REMOVE to remove to your list.\n";
    cout << "3. LIST to view your list.\n";
    cout << "4. CLEAR to erase your list.\n";
    cout << "5. END the program.\n";
    cin >> choice;


    switch (choice)
    {

        case ADD:
            {
            cout << "\nAdding to your list of games.\n";
            cout << "If you wish to exit and return to the selection screen, type 'exit'\n";


            string answer;
            while (answer != "exit")
            {
                cout << "\nType the name of the game you wish to add.\n";
                cin >> answer;
                games.push_back(answer);
            }
            break;
            }

        case REMOVE:
            {
                break;
            }

        case LIST:
            {
            cout << "\nHere's your list of games.\n";
            int pos = 0;
            for (iter = games.begin(); iter != games.end(); ++iter)
            {
                ++pos;
                cout << pos <<"."<< *iter << endl;
            }
            break;
            }

        case CLEAR:
            {games.empty();
            }

        case END:
            {goto end;
            }

        default:
            {cout << "Invalid option\n";
            }
    }
goto start;
end:

    return 0;
}
When you goto start again you are redefining

enum choice {ADD = 1, REMOVE = 2, LIST = 3, CLEAR = 4, END = 5};
vector<string> games;
vector<string>::const_iterator iter;
int choice;

put them before start. And by the way, try to avoid the goto keyword. its evil. Try do this with a while loop instead.

Wish you best luck :)
Oh, I see. Thanks a lot for the help, as well as the tip. I'll figure out how I'll replace goto with a while loop as suggested, and finish up the rest of the program.

Thanks again.
Topic archived. No new replies allowed.