C++ with vectors

I am trying to use vectors(in Visual Studio 2008) to make a program that asks how many favorite games you've got, then to list the games after typing them in.
Here's my code without using vectors, and it works. but i need to use vectors. I dont really understand vectors yet but if someone can help me make this I would be forever grateful.
Thank you.

#include <iostream>
#include <string>

using namespace std;
void main()
{

string blah;
string gameList;
cout << "how many?\n";
cin >> blah;
int i = atoi(blah.c_str());

while(i>0)
{
string game;

cout << "\nplease enter game " << i << " :";
cin >> game;

gameList += game + ", ";

i--;
}
cout << "Your favorite games are: " << gameList << endl;
}
Well first, you should use [code](code goes here)[/code] tags and indentation to make your code readable.
Secondly, main must return int. void main() is incorrect.

string blah;
string gameList;
cout << "how many?\n";
cin >> blah;
int i = atoi(blah.c_str());

What is the use of taking an std::string as input and converting it into a C string, and then into an integer? Why not just take an integer directly or, better yet, use a stringsteam? http://www.cplusplus.com/reference/iostream/stringstream/

1
2
3
4
5
6
7
8
string game;

cout << "\nplease enter game " << i << " :";
cin >> game;

gameList += game + ", ";

i--;

I wouldn't recommend using operator >> like this with strings. You can use std::getline(std::cin, game);

Anyway; you need to have a vector to use vectors. You should #include <vector> and then create a vector object:
std::vector<std::string> games;

Whenever you get a string as input you can use std::vector::push_back() to append the value to the vector, or you can use std::vector::insert() to add the value to a specific location. I'd use push_back() for now, because if you decide to remove the value, provided you haven't recently push_back()'d anything else, you can do a pop_back() to remove the value.

Note: with a vector, there'll be no need for asking how many games the user is going to enter, because you can just keep adding to it. You should still have a limit, though; because otherwise you'll run out of usable memory.
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
	std::vector<std::string> game;
	string faves;
	string gameList;
	cout << "How Many Favorite Games Do You Have?\n";
	cin >> faves;
	int i = atoi(faves.c_str());
	
	while(i>0)
	{
		string game;

		cout << "\nPlease Enter game names " << i << " :";
		std::getline(std::cin, game);

		gameList += game + ", ";

		i--;
	}
	cout << "Your favorite games are: " << gameList << endl;
}


like this? that was my attempt...lol i dont know where to put the stringstream. sorry about the code thing i didnt know how to do it. the way it is now it skips a number when you run it.
Well, now you're storing a string in a vector. That can't work :l

Here's an example of storing ten strings in a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<std::string> lines; // Here we declare a vector capable of storing an undefined amount of std::strings
const int max_strings = 10; // We'll use this as the maximum amount of strings to take from the user

// Here we take at most max_strings strings from the user:
for (int i = 0; i < max_strings; i++) {
    std::string line = ""; /* You shouldn't do this in C++ in a loop in speed-critical code;
                            * calling the constructor and destructor for the std::string would slow
                            * down the code a little much, but for us, it's fine. */

    std::cout << "Enter some text: "; // Prompt the user for input
    std::getline(std::cin, line); // Here we actually take the string from the stdin buffer
    std::cout << std::flush; // Newline drawn after getline()

    lines.push_back(line); // And here we add the line to the end of the vector
}


Do you see how that works?

Note: you don't need the std:: because you're using using namespace std; already. Personally I don't like to do that, but as long as you don't do it in header files, you should be OK.
this is what my teacher told me so far what to do but i cant get any further...
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
	vector<string> games;
	string faves;
	string gameList;
	cout << "How Many Favorite Games Do You Have?\n";
	cin >> faves;
	int i = atoi(faves.c_str());
	
	while(i>0)
	{

		cout << "\nPlease Enter game names " << i << " :";
		cin >> gameList;
		games.push_back(gameList);

		i--;
	}

while
I've given you most of the code; you just need to understand it and adapt it.

You shouldn't use operator>> for string input; use getline():
1
2
getline(cin, game);
games.push_back(game);
Last edited on
Yeah I still don't understand it, can you make it more clearer at all if possible? I just started C++ last week and still trying to get it.
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
	vector<string> games;
	string faves;
	string game;
	cout << "How Many Favorite Games Do You Have?\n";
	cin >> faves;
	int i = atoi(faves.c_str());
	
	while(i>0)
	{

		cout << "\nPlease Enter game names " << i << " :";
		cin >> game;
		games.push_back(game);

		i--;
	}
	typedef vector<string>::size_type vec_sz;
	vec_sz g= 0;

	while(games.size() > g)
	{
		cout << games[g];
		g++;
}
	cout << endl;
}


how do i make the list at the end put a comma?
Let me walk through your code, highlighting things you should change and improve.

1
2
3
4
5
	string faves;
	string game;
	cout << "How Many Favorite Games Do You Have?\n";
	cin >> faves;
	int i = atoi(faves.c_str());

As I said already, you don't need all this. You can define a variable to set a limit for the maximum amount of games, e.g. const int max_games = 10; and then an iterator to get at most ten games. You can tell the user to enter "<finished>" or something when they're done, and then check if the string is equal to that. If it is, you stop, otherwise, you add it to your list and then continue.

1
2
3
4
5
6
7
8
9
	while(i>0)
	{

		cout << "\nPlease Enter game names " << i << " :";
		cin >> game;
		games.push_back(game);

		i--;
	}

I've already commented on this. There's no need to use a while loop with a variable that stays in scope after the loop is completed. I've also already said not to use cin >>; but to use getline().

1
2
typedef vector<string>::size_type vec_sz;
	vec_sz g= 0;

You have no need to typedef that. It doesn't make the code any clearer or easier to read, and having a typedef within a function is not a good idea.

1
2
3
4
5
	while(games.size() > g)
	{
		cout << games[g];
		g++;
}

You could use a for loop to make this better, too:
1
2
3
for (vector<string>::size_type sz = 0; sz < games.size(); sz++) {
    /* ... */
}


And then at the end, you draw a newline and flush the buffer, exactly like you have done. You should probably put a return 0; at the end of the main() function. It's not exactly required, most compilers will do it for you; but it makes your code more sensical and generally better.

how do i make the list at the end put a comma?

You mean comma separate each value? Do you want to store it with the comma or just print it with the comma? I'd recommend you to print it with the comma rather than storing the comma; you could just do cout << games << ", ";.

Do you want to remove the newline at the end of each string? I believe getline() will add one. You can use std::string::replace() for that: http://www.cplusplus.com/reference/string/string/replace/
Last edited on
sweet i got it all figured out now. thank you so much for your help....now to re make it using functions...lol
Good luck.
Topic archived. No new replies allowed.