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?
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
usingnamespace 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;
}
elseif (choice=='2') //add a game
{
cout<<"Please enter the game title. ";
cin>>gameTitle;
library.push_back(gameTitle);
cout<<endl;
}
elseif (choice=='3') //remove a game
{
cout<<"Please select which game to remove from your library."<<endl;
for (unsignedint 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;
}
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.
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!