Creating a list for my favorite games.

Hey guys,

Well I've been programming now for about a month and this is an exercise in the book I'm reading. It works fine but if I input a string when trying to remove a game that isn't found among the vector elements, the program just crashes everytime.

I used an if statement that compares if "removeGame" is equal to the element "gameIter" referenced.

I'll put the code in bold where I'm having the issue.

Any advice would be golden.

Kind regards.

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
// A program that controls a list of my favourite games
// When you get up, try splitting up the switch statement into seperate functions/methods
// Try completing the "remove a game" case. == Completed
// Implement number of games. == Partial

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

using namespace std;

int main(){

	
	string addGame, removeGame;
	vector<string> gameList;
	vector<string>::iterator gameIter;

	gameList.push_back("Call Of Duty");
	gameList.push_back("Fifa");
	gameList.push_back("Minecraft");
	gameList.push_back("WarZ");
	gameList.push_back("Skyrim");
	gameList.push_back("Metro 2033");
	gameList.push_back("League Of Legends");
	gameList.push_back("Loadout");

	int numberOfGames = gameList.size();
	int userSelection = 0;

	cout << "Welcome to your Game List\n" << endl;

	while (userSelection != 4) {

		cout << "Press 1 to add a game\n";
		cout << "Press 2 to remove a game\n";
		cout << "Press 3 to list all games\n";
		cout << "Press 4 to quit\n" << endl;
		cout << "Type your option: ";
		cin >> userSelection;

		switch (userSelection) {
		case 1:												// Add a title!
			cout << "\nType the names title to add: ";
			cin.get();
			getline(cin, addGame);
			gameList.push_back(addGame);
			sort(gameList.begin(), gameList.end());
			cin.clear();
			cin.sync();
			numberOfGames += 1;
			cout << "\nGame successfully added.\n" << endl;
			break;
		case 2:												// Remove a title!
			cout << "\nType the names title to remove: ";
			cin.get();
			getline(cin, removeGame);
			gameIter = find(gameList.begin(), gameList.end(), removeGame);
			if (removeGame == *gameIter) {
				cout << "\nTitle found!" << endl;
				gameList.erase(gameIter);
				numberOfGames -= 1;
				cout << "Title removed!\n" << endl;
			}
			else {
				cout << "Title not found\n";
			}
			break;
		case 3:
			cout << "\nYou currently have " << numberOfGames << " games in your list.\n" << endl;
			for (gameIter = gameList.begin(); gameIter != gameList.end(); ++gameIter) {
				cout <<"\t"<< *gameIter << endl;
			}
			cout << "\n";
			break;
		case 4:
			cout << "Program shutdown.\n" << endl;
			userSelection = 4;
			break;
		default:
			cout << "\nThat is not a valid selection.\n" << endl;
			break;
		}

	}

	return 0;
}
Last edited on
closed account (3hM2Nwbp)
std::vector<T>::find will return std::vector<T>::end() if nothing was found.
template <typename InputIterator, typename T>
InputIterator find (InputIterator first, InputIterator last, const T& val) will return last if nothing was found.

* Mixed up the standard library for a second there!

To rectify your bug @ line 63:

if (gameIter != gameList.end()) {

Also, I'd recommend removing line 11, as you're not using any constructs from the <windows.h> header.
Last edited on
Thanks for that.

I was using sleep function before but I forgot to remove the header for it.

Thanks again!

Last edited on
Topic archived. No new replies allowed.