Storing words in a string vector.

I'm having a problem with a program.
How can i store two words in the addGame vector? When in case 2 of the switch, if i try to add like "hello world" without the quotation marks the program flips out and starts looping.
When i debug addGame is only storing the first of the words, like in the example
only "Hello".

I can add single words without a problem. And if i add something to addGame in the code with push_back it can store both words with the space in between them.
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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
	vector<string> gameList;
	vector<string>::const_iterator iter;
	string addGame;
	int removeNumber;
	int choice;

	cout << "Welcome to your personal game list!\n" << endl;

mainMenu:
	int number = 0;

	cout << "\n1. View the list." << endl;
	cout << "\n2. Add a game to the list." << endl;
	cout << "\n3. Remove a game from the list.\n\n" << endl;
	
	cout << "Choose: ";
	cin >> choice;

	switch(choice)
	{
	case 1: for(iter = gameList.begin(); iter != gameList.end(); iter++)
			{
				cout << "\n" << *iter << endl;
			}
			goto mainMenu;

	case 2: cout << "\nWrite the title of the game:"; 
			cin >> addGame; //Here is where it fucks up in the program if i add a two word title.

			gameList.push_back(addGame);
			sort(gameList.begin(), gameList.end());
			cout << "\nGame added succesfully." << endl;
			
			goto mainMenu;
			
	case 3:
			for(iter = gameList.begin(); iter != gameList.end(); ++iter)
			{
			++number;
			cout << number << ". " << *iter << endl;	
			}

			cout << "\nType the number of the game you want to remove below." << endl;
			cout << "Remove number: ";
			cin >> removeNumber;

			gameList.erase((gameList.begin() + removeNumber - 1));

			cout <<"\nGame removed." << endl;
			number = 0;
			for(iter = gameList.begin(); iter != gameList.end(); ++iter)
			{
			++number;
			cout << number << ". " << *iter << endl;	
			}
			
			goto mainMenu;
	}
	return 0;
}

Quote from the reference on this site about the extraction operator


operator>>

istream& operator>> (istream& is, string& str);

Extract string from istream

Extracts a string from the input stream is storing its content in str. Any previous content of str is cleared.

This function overloads the global operator>> to behave as described in istream::operator>> but applied to string objects.

Notice that the istream extraction operations use whitespaces as separators, therefore this operation will only extract what can be considered a word from the stream. To extract entire lines of text, refer to the string overload of global function getline.


http://cplusplus.com/reference/string/operator%3E%3E/
http://cplusplus.com/reference/string/getline/
Last edited on
You could probably use a char array instead of a string. I believe that would work but i don't know to much about vectors.
His problem has nothing to do with his choice to use strings nor his choice to use a vector (though there could have been a better choice of container). The problem was caused by his use of the extraction operator to get input from cin.
Last edited on
Thanks!

Works like a charm now.

Had to put a cin.ignore() before the getline() too so it wouldn't skip it.
1
2
3
4
5
6
7
8
9
case 2: cout << "\nWrite the title of the game:"; 
                cin.ignore();
		getline(cin,addGame);

		gameList.push_back(addGame);
		sort(gameList.begin(), gameList.end());
		cout << "\nGame added succesfully." << endl;
			
		goto mainMenu;

Last edited on
Topic archived. No new replies allowed.