Help with multiple strings in one variables slot.

So I got a quick question here. So on the first 'else if' statement I want the user to enter a game name but it freaks out when they use multiple words. For example 'Gears of War'. It repeats the next part 3 times. How would I fix this so it can store that whole name into one slot?

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
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
	char choice, decision;
	vector<string> library;
	string gameTitle;
	
	while (decision !='n')
	{
		cout<<"What would you like to do?\n";
		cout<<"(1)List your game library.\n";
		cout<<"(2)Add a game to your library.\n";
		cout<<"(3)Remove a game from your library.\n";
		cin>>choice;
		
		if (choice=='1') //list the games
		{
			for (int i=0;i<library.size();i++)
			{
				cout<<"("<<i+1<<")"<<library[i]<<endl;
			}
			cout<<endl;
		}
		else if (choice=='2') //add a game
		{
			cout<<"Please enter the game title. ";
			cin>>gameTitle;
			library.push_back(gameTitle);
			cout<<endl;
		}
		else if (choice=='3') //remove a game
		{
			cout<<"Please select which game to remove from your library."<<endl;
			for (unsigned int i=0; i<library.size();i++)
			{
				cout<<"("<<i+1<<")"<<library[i]<<endl;
			}
			int game;
			cin>>game;
			game=game-1;
			library.erase(library.begin()+game);
			cout<<endl;
		}
		
		cout<<"Would you like to continue?(Y/N)";
		cin>>decision;
		cout<<endl<<endl;
	}
	
	cout<<"\n\nThank you for using this program!";
	
	return 0;
}

Change your cin on line 32 to:

getline(cin, gameTitle);

After the cin on line 19 you will be left with newline in the stream buffer which will cause a skip-over so you will also need to add the following after the cin at line 19

cin.ignore(1, '\n');


Basically cin ignores whitespace and the newline therefore if you enter Joe Bloggs it will see only Joe and Bloggs\n will be left in the stream. getline doesnt ignore the whitespace or newline.


EDIT: heres some links which may help:

http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/iostream/cin/
http://www.cplusplus.com/reference/istream/istream/ignore/
Last edited on
Softrix wrote:
cin.ignore(1, '\n');
Why not simply do cin.ignore(); if you wish to ignore only the next character? Though I would just ignore anything left in buffer with std::cin.ignore(std::numeric_limits<std::stream_size>::max(), '\n'); or even something like std::cin.ignore(1024, '\n'); though I tend not to use magic numbers.
I tried what you said Softrix and it worked. I got the results I'm looking for. Thank you very much for your help. I looked into it some more with those links and it makes sense. I appreciate the help!
To giblit I'm still new to programming, barely like my 4th week in this class so I am a bit confused reading yours only because I haven't learned some of that stuff. I'm sure it would work as well though and I appreciate your help.
Thank you two!

Your welcome.
Topic archived. No new replies allowed.