-
Notifications
You must be signed in to change notification settings - Fork 0
/
H04_1.cpp
122 lines (85 loc) · 2.82 KB
/
H04_1.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// H04_1: Favorite Game List Maintainer using Vectors and Iterators
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//Declare Vector and Iterator
vector<string> favoriteGames;
vector<string>::const_iterator iter;
//Greet the user
cout << "Welcome to the favorite game maintainer!" << endl << endl;
//Loop the menu choice until the user quits from inside the loop
while (true)
{
int menuChoice;
//Display the menu options
cout << "1. Add game to the favorites list" << endl;
cout << "2. Remove game from the favorites list" << endl;
cout << "3. List games in the favorites list" << endl;
cout << "4. Quit" << endl;
//Get the option choice from the user
cin >> menuChoice;
cout << endl;
switch (menuChoice)
{
case 1:
{
//Declare the string variable that will hold the name of one of the user's favorite games
string gameName;
//Get the user's favorite game (without spaces) and add it to the vector
cout << "Enter the name of the game you would like to add to the list of favorites (No spaces allowed): " << endl;
cin >> gameName;
favoriteGames.insert(favoriteGames.begin(), gameName);
cout << endl;
}
break;
case 2:
{
int gameNum;
int currentNum = 1;
//List the games available to delete
cout << "List of favorite games:" << endl << endl;
for (iter = favoriteGames.begin(); iter != favoriteGames.end(); ++iter)
{
cout << currentNum << ": " << *iter << endl;
currentNum++;
}
cout << endl;
//Allow user to select which game they want to erase from the list
cout << "Enter the number of the game you would like to remove from the list of favorites:" << endl;
cin >> gameNum;
favoriteGames.erase(favoriteGames.begin() + (gameNum - 1));
cout << endl;
}
break;
case 3:
{
//List all the games contained in the vector
cout << "List of favorite games:" << endl << endl;
for (iter = favoriteGames.begin(); iter != favoriteGames.end(); ++iter)
{
cout << *iter << endl;
}
cout << endl;
}
break;
case 4:
{
//Quit the program
return 0;
}
break;
default:
{
//Tell the user their choice is invalid
cout << "Invalid choice!" << endl << endl;
}
break;
cout << endl << endl;
}
}
return 0;
}