Vectors and GetLine

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.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Your very own video game listing
// List your favorite games!!!!!

#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>

using namespace 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;   
    
    
}
Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
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.
Last edited on
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.
Topic archived. No new replies allowed.