Dynamic size of structure members

Apr 26, 2009 at 2:30pm
Hello!

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:

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_GAMES 3
string numbers[10]= {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth", "tenth"};

	struct games {
	  string title;
	  string genre;
	  int rating;
	} gamea [N_GAMES];

void printgame(games game);

int main ()
{
  string stuff;

  gamea[0].title = "Mario";
  gamea[0].genre = "Platformer";
  gamea[0].rating = 10;

  for (int i=1; i<N_GAMES; i++){
  cout << "Enter " << numbers[i-1] << " game's name: ";
  getline (cin, gamea[i][i][/i].title);
  cout << "Enter " << numbers[i-1] << " game's genre: ";
  getline (cin, gamea[i][i][/i].genre);
  cout << "Enter your rating: ";
  getline (cin, stuff);
  stringstream (stuff) >> gamea[i].rating;
  }

  cout << "My game:\n";
  printgame (gamea[0]);
  for (int i=1; i<N_GAMES; i++){
  cout << "Your " << numbers[i-1] << " game is:\n";
  printgame (gamea[i][i][/i]);
  }
  return 0;
}

void printgame (games game){
	cout << game.title << " (" << game.genre << ")\n";
	cout << "Rating: " << game.rating << "\n";
}


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?
Apr 26, 2009 at 2:52pm
Keep in mind that you can't use new or delete in global scope. It has to be inside of a function:
1
2
3
4
games *gamea=new games[/*size goes here*/];
//...
delete[] gamea;
//using "delete gamea;" would cause a memory leak. 
Apr 26, 2009 at 3:01pm
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);
}
Apr 26, 2009 at 4:16pm
So I can make new members to the stuct on the fly. I didn't know that :$

I deleted the constant and inserted this into the beginning of my main():

1
2
3
4
5
6
7
int N_GAMES;
  string stuff;
  cout << "number of games: ";
  getline (cin, stuff);
  stringstream (stuff) >> N_GAMES;
  N_GAMES++;
  games* gamea = new games[N_GAMES];


it works like a charm now :) Thanks for the tips.

Now, if only I could do it with linked lists...
Apr 26, 2009 at 4:24pm
You can. See std::list<>

http://cplusplus.com/reference/stl/list/

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.
Topic archived. No new replies allowed.