My program is supposed to take in a video game title entered by the user and input it into a vector using push_back. It works not as I want. The problem is when I don't use getline in my loop you can put in the game titles in but you can't put spaces. When I do use getline, it keeps asking whether or not you want to add another game without letting you add another game. Any other bugs I can fix but this is the main one I'm trying to get worked out.
// Your very own video game listing
// List your favorite games!!!!!
#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
usingnamespace std;
int main()
{
vector<string> gameTitles; // vector containing all of the game titles
vector<string>:: iterator gameIterator; // used to itrate through the gameTiles vector
string userEntry = " "; // used to add entries into the vector
int addAnother ; // used to determine when to exit initial title loop
cout << "Welcome beloved gamer!!! Enter your favorite games into your\n";
cout << "very own customized game list!!!\n";
getline(cin, userEntry);
gameTitles.push_back(userEntry);
cout << "Would you like to add another game to your list?\n";
cout << "1 for yes 0 for no\n";
cin >> addAnother;
while (addAnother <0 || addAnother >1)
{
cout << "Ooops invalid entry please 1 for yes 0 for no\n";
cin >> addAnother;
}
do
{
cout << "Add next game\n";
cout << "Enter your next game\n";
getline(cin, userEntry);
gameTitles.push_back(userEntry);
cout << "Add another???\n\n 1 for yes 0 for no";
cin >> addAnother;
} while (addAnother == 1);
for (gameIterator = gameTitles.begin(); gameIterator != gameTitles.end(); ++gameIterator)
cout << *gameIterator << endl;
return 0;
}
After your cin >> addAnother; you need a cin.ignore(numeric_limits <streamsize>::max(), '\n');. You have a trailing '\n' after that cin which the getline() reads and enters empty text.
You'll need to #include <limits> for numeric_limits.
Thank you wolfgang and thank you PanGalactic. I am a first time poster so now I am aware of the code tags. I'm sure there will be more errors to come. Wolfgang thanks I that is beyond the scope of my book but it works like a charm! I will put it into my notes because more than likely I will run into this problem again.