Hi, i was doing one of the exercises in a book i have. The exercise goes like this:
Write a program using vectors and iterators that allows a user to maintain a list of his o hers favorite games. The program should allow the user to list all game titles, add a game title, and remove a game title.
It wasn't very hard to make but i got one problem. Here is the code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespace std;
int main()
{
vector<string> games;
vector<string>::const_iterator iter;
vector<string>::iterator myIterator;
bool running = true;
///*** Start of program ***///
while(running)
{
char choice;
cout << "1:List games" << endl;
cout << "2:Add game" << endl;
cout << "3:Remove game" << endl;
cout << "Choice: ";
cin >> choice;
///*** List all the games ***///
if(choice == '1')
{
///*** If there are no games to list ***///
if(games.empty())
{
cout << "\n\nThere are no games\n\n";
}
else
{
cout << "\n\nGames:\n";
for(iter = games.begin(); iter != games.end(); ++iter)
cout << *iter << endl;
cout << "\n\n";
}
}
///*** Add a game ***///
if(choice == '2')
{
string game;
cout << "\n\nAdd game: ";
cin >> game;
games.push_back(game);
cout << "\n\n";
}
///*** Remove a game ***///
if(choice == '3')
{
string game;
cout << "\n\nRemove game: ";
cin >> game;
myIterator = find(games.begin(), games.end(), game);
if(myIterator != games.end())
games.erase(myIterator);
cout << "\n\n";
}
}///*** End of 'running' loop
return 0;
}
Now the problem is if i want to add a game like 'Super Mario' it will only add
'Mario' because i use cin >>. When i try with getline() the program just jumps over that part of the code and takes me back to my 3 choices. Anyone got an ide how i could add a game like 'Super Mario' for example?
Thanks in advance!