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/