Help with increasing vector by user input

Here's my program so far:
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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	vector<string>favGames(5);
	string userInput;

	cout << "Please enter 5 of your favourite games (Starting with Most Fav): " << endl;
	cin >> favGames[0];
	cin >> favGames[1];
	cin >> favGames[2];
	cin >> favGames[3];
	cin >> favGames[4];

	for(int i = 0; i < favGames.size(); ++i)
	{
		cout << "Your chosen games: " << favGames[i] << endl;
	}

	cout << "\nEnter any game you may have forgotten: " << endl;
	favGames.insert(favGames.begin(), "Skyrim");

	cout << "Your items: " << endl;
	for(int i = 0; i < favGames.size(); ++i)
	{
        cout << favGames[i] << endl;
	}

	cout << favGames.size();

}

So the aim of my program is the user can enter their favourite games, and add or remove their games. But I am having a problem with the user entering any further games.
If you look at line 25, that's where I'm stuck (I just made it insert Skyrim to see if it would work - and it did) However, I want to make so that the user can enter a game. I've tried cin >> favGames.insert(fav.begein), " ") But that didn't work, and I've tried putting it in a for loop. And I either get an error to with the >> or the . bit.
Line 33 is just something I did to see if outputted "6" so I know the vector size has increased - it did.
I appreciate any help :D
I also thought I might have to use iterators, but I'm still not 100% on them.
Last edited on
To add more than 5 games you will need to increase the size of your vector by either using the push_back(), insert() methods or resize() your vector to a larger size.
If I'm not mistaken, the []-operator for the std::vector class can't be used to actually store elements in the vector. You have to use push_back for that.

EDIT: Instead, try
1
2
3
cout << "Please enter 5 of your favourite games (Starting with Most Fav): " << endl;
cin >> userInput;
favGames.push_back(userInput);


Or something like that :)
Last edited on
Thanks guys, I forget to mention that I did know and try about push_backs(); But I just didn't know how to use them so that it would store elements inputted by the user.
Topic archived. No new replies allowed.