I'm still learning C++, and I got stuck learning structures. I'm reading the tutorials on the site, and I found the one about structs. I started playing around with struct, but I got stuck when I tried to use dynamic arrays for structure members. Here is my code:
I still have the number of games as a constant. I wanted to make it so, that the user would be able to enter the number of games he wishes to enter (up to ten). How can I make the gamea [N_GAMES] member dynamically sized?
I tried to use "new" and "delete" to assign the size, but failed. Can someone please help me out here?
You can ask for the number of games and assign the to numberOfGames. Then do:
games* gamea = new games[numberOfGames];
Just don't forget to delete[] gamea;
A better solution is to use vector:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <vector>
struct Game
{
...
};
typedef vector<Game> Games;
Games games;
while (/* user is adding games */)
{
Game game;
// Get game info...
games.push_back(game);
}
The benefits to using container classes is that they will automatically clean themselves up, whereas when you use new, you have to follow up with delete. Plus you know the container classes work, whereas you have to debug anything you write on your own.