Help Adding 2 word choice

I am working on this program for class. I have it working fine, but when I try to add a game with 2 words it will only add the first word. What am I missing?

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
int main()
{
int quit = false;
string remove;
string gameAddition;
vector<string> gameName;
vector<string>::iterator myiterator;
vector<string>::iterator iter;

    cout << "A simple way to remember your favorite games.\n";

//main loop
while (!quit)
{
    string choice;
    cout << "\nEnter a number for your selection choice?\n";
    cout << "\n1. Add a game.\n";
    cout << "\n2. Remove a game.\n";
    cout << "\n3. List all Games.\n";
    cout << "\n4. Stop and exit.\n";
    cout << "\nEnter your selection: ";
    cin >> choice;

    if (choice == "1")
    {

        cout << "\nEnter Game title: ";
        cin >> gameAddition;
        gameName.insert(gameName.end(), gameAddition);


    }
    else if (choice == "2") 
    {

        cout << "\nRemove for a title: ";
        cin >> remove;

        iter = find(gameName.begin(), gameName.end(), remove);
        if (iter != gameName.end())
        {
            cout << "\n" << *iter << " was found and deleted.\n";
            gameName.erase(iter);
        }
        else
        {
            cout << "\nGame not found.\n";
        }
    }
    else if (choice == "3")
    {
        //List Game titles 
        cout << "\nList of Game titles.\n";
        for (iter = gameName.begin(); iter != gameName.end(); ++iter)
        {
            cout << *iter << endl;
        }
    }
    else if (choice == "4")
    {
        cout << "\nPeace out!!\n" << endl;
        quit = true;
    }
    else
    {
        cout << "\nInvalid choice, please choose again.\n";
    }
}
return 0;
    }
Last edited on
If you want to gather more then one word you need to use getline instead of cin.
More info: http://www.cplusplus.com/reference/string/string/getline/

Example:
1
2
3
4
	cout << "Please, enter your full name: ";
	cin.ignore(80, '\n');
	getline(std::cin, name);
	cout << "Hello, " << name << "!\n";


If you use cin before getline you will need the cin.ignore, if you don't, like in this example, it is not necessary.
Thanks Joe. I just found that as well and got it working :)
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
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<ctime>
#include<cctype>
using namespace std;

int main()
{
	int quit = false;
	string remove;
	string gameAddition;
	vector<string> gameName;
	vector<string>::iterator myiterator;
	vector<string>::iterator iter;


	cout << "A simple way to remember your favorite games.\n";

	//main loop
	while (!quit)
	{
		string choice;
		cout << "\nEnter a number for your selection choice?\n";
		cout << "\n1. List all Games.\n";
		cout << "\n2. Add a game.\n";
		cout << "\n3. Remove a games.\n";
		cout << "\n4. Stop and exit.\n";
		cout << "\nEnter your selection: ";
		cin >> choice;

		if (choice == "1")
		{
			cout << "\nList of Game titles.\n";
			for (iter = gameName.begin(); iter != gameName.end(); ++iter)
			{
				cout << *iter << endl;
			}
		
		}
		else if (choice == "2") 
		{
	
			cin.ignore();
			cout << "\nEnter Game title: ";
			getline(cin, gameAddition);
			gameName.insert(gameName.end(), gameAddition); 
		}
		else if (choice == "3")
		{ 
			cin.ignore();
			cout << "\nRemove for a title: ";
			getline(cin, remove);

			iter = find(gameName.begin(), gameName.end(), remove);
			if (iter != gameName.end())
			{
				cout << "\n" << *iter << " was found and deleted.\n";
				gameName.erase(iter);
			}
			else
			{
				cout << "\nGame not found.\n";
			}
		}
		else if (choice == "4")
		{
			cout << "\nPeace out!!\n" << endl;
			quit = true;
		}
		else
		{
			cout << "\nInvalid choice, please choose again.\n";
		}
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.