String Producing Strange Result

How would I combine user input of multiple words into one string? I'm creating a program that asks users to type an entry which is then added to a string vector but if there are spaces in the string entered it counts it as multiple strings and adds each string to the vector rather than the whole phrase as one string. Is there any way to ensure that multiple words are stored in the vector element?
Last edited on
Thanks, very helpful. But now for some reason the program is displaying the prompt to enter data twice. Not sure why:

1
2
3
4
5
6
7
8
{
			cout << "\nEnter a game to add to the list: ";
			getline(cin, gameChoice);
			games.insert(games.end(), gameChoice);
			if (gameChoice == "menu")
				games.erase(games.end() - 1);
		}
		break;


This is part of the switch case I'm using to enter data, remove data or just view the data in the list but line 2 seems to show up twice the first time round the loop.
Last edited on
To clarify, it's adding a blank entry to index[0] of my vector automatically but I can't see why.
I don't see anything wrong with this code. Post all/more of it.
What types are 'games' and 'gameChoice' ? vector<string> and string?

You could slightly simplify your code:
1
2
getline(cin, gameChoice);
if (gameChoice != "menu") games.insert(games.end(), gameChoice);//why not push_back? 
Well I've fixed that particular issue, it was because I was mixing cin and getline so the \n was left in the stream and caused a blank entry. I have a solve for that for now by using a stream to convert numeric strings into ints so I don't have to use cin for it. Yeah, probably would have been smarter to use that simplified version but it's not push back simply because I'm planning to implement a feature where the user can insert the item at a place of their choice in the list so it's set up so all I need to do is add that option then I'll change the argument passed to (games.begin + position) or something.

Issue I'm having now is in another post I made and it contains the full program and involves a feature to erase items in the list and the fact that every time I choose an item to erase I'm given the assertion
Expression:vector iterator + offset out of range


http://www.cplusplus.com/forum/beginner/27535/
Topic archived. No new replies allowed.