Game Listing Program with vectors and iterators

Hello everyone, I'm currently working with exercises in C++ with vectors and iterators to go with them as well. This program I've written works well, only things I have trouble with is for the user input with vectors. Like if I use getline(cin, string); without vectors I can get the whole game title fine but its not working here.

So instead i'm just using this and it can one get a string without any spaces :
1
2
3
4
5
6
7
8
9
10
              case ADD:
			cout << "\nName of title to add:\n";
			cin >> gameToAdd;

			if (!gameToAdd.empty()) {
				favoriteGameTitles.push_back(gameToAdd);
				cout << "\nGame title successfully added!\n";
			} else
				cout << "\nInvalid game title\n";
			break;


And if anyone can point to me any mistakes that should not be in here please do tell, I want to make the most improvements as I can. Thank you.

My whole class:
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>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

	// Holds the current states for the options
	enum states { NONE, LIST, ADD, REMOVE, QUIT };

	// Holds the player option input
	int option = NONE;

	// Iterators to iterate through the game titles
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;

	// A collection of strings that holds the user's game titles
	vector<string> favoriteGameTitles;

        // Option menu
	cout << "Welcome to your personal favorite game titles!\n\n";
	cout << "1.List your current game titles\n";
	cout << "2.Add a game title\n";
	cout << "3.Remove last game title added\n";
	cout << "4.Quit program\n";

	// Enter the program loop
	while (option != QUIT)
	{

		// Get the players option input
		cout << "\nOption: ";
		cin >> option;

		string gameToAdd;

		switch (option)
		{
		case LIST:
			cout << "\nYour current game titles:\n";
			if (favoriteGameTitles.empty()) // player has no game titles added so display empty message
				cout << "You have no titles added.\n";
			else
			for (iter = favoriteGameTitles.begin(); iter != favoriteGameTitles.end(); ++iter) // print the player's game titles
					cout << *iter << endl;
			break;
		case ADD:
			cout << "\nName of title to add:\n";
			cin >> gameToAdd;

			if (!gameToAdd.empty()) {
				favoriteGameTitles.push_back(gameToAdd);
				cout << "\nGame title successfully added!\n";
			} else
				cout << "\nInvalid game title\n";
			break;
		case REMOVE:
			if (!favoriteGameTitles.empty()) {
				favoriteGameTitles.pop_back();
				cout << "\nGame title successfully removed!\n";
			} else
				cout << "\nYou have no game titles to remove!\n";
			break;
		case QUIT: // exits main program loop
				cout << "\nThanks for using this program!\n";
			break;
		default: cout << "\nInvalid option.\n";
		}
	}

	return 0;
}


Also should I be using maps instead of vectors? I want the user to be able to take out a title from maybe the middle of the collection and not just from the bottom of the stack everytime with pop_back();
Last edited on
> Like if I use getline(cin, string); without vectors I can get the whole game title fine

you can use getline() with vectors too.

1
2
3
4
5
6
7
8
9
10
11
              case ADD:
			cout << "\nName of title to add:\n";
			// cin >> gameToAdd;
                        std::getline( std::cin, gameToAdd ) ;

			if (!gameToAdd.empty()) {
				favoriteGameTitles.push_back(gameToAdd);
				cout << "\nGame title successfully added!\n";
			} else
				cout << "\nInvalid game title\n";
			break;



> Also should I be using maps instead of vectors?

A std::set<>, you don't need a std::map<> unless you are associating some data with the key (the game title). It would be more time efficient if there are a large number of game titles.

Do it any way, have two versions of your program; it is good experience.
Thanks so much for helping me out here! I really do appreciate everyone that does help me. And by the way what do you mean two versions? Save this first version, and make a second one? =] and what is a key :/

EDIT: getline() function didn't work. Right when I entered the option 2, to enter a game title. It's output was :
1
2
3
4
5
Option : 2
Name of title to add: 
Invalid game title

Option :
Last edited on
After int option ; std::cin >> option ;, the newline (or some white space) following the int is still present in the input buffer.

std::getline( std::cin, gameToAdd ) finds this new line and returns an empty line.

Solution: eat up the remaining characters in the input buffer before the getline().
std::cin.ignore( 1024*1024 ) ;
http://www.cplusplus.com/reference/iostream/istream/ignore/


> Save this first version, and make a second one?

Yes. Make sure that your first version is working; save it. Then make a second version: copy the first version and modify it to use a set<>


> what is a key

See: http://www.cplusplus.com/reference/stl/map/
Last edited on
Thank you again! And why did you put :
cin.ignore(1024*1024);
It didn't work lol, but when I made it to :
cin.ignore(1);
It worked perfect, maybe you just put it there for me to figure out =] thanks though.
cin.ignore(1); will ignore just one character. You want to ignore everything upto and including a new line.

Perhaps your implementation needs something like cin.ignore( 1024*1024, '\n' );..
Topic archived. No new replies allowed.