Problem with dynamic allocation of a struct.

The first allocation was working well but when I coded the second allocation, it is showing an error: error C2143: syntax error : missing ';' before '.' (The dot was from Products *newProd.prod_name = new Products[prod_size];)
Can you tell me what's wrong with this? I don't really know how to allocate multiple structs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void INPUTPROD(int val)
  {
	Products *newProd = new Products[val];
	const int prod_size = 100;
	for (i = 0; i < val; ++i){
		cout << "Product" << i+1 <<": ";
		cin >> newProd[i].prod_name;

		int prod_size;
		cout << "How many " << newProd[i].prod_name << "? ";
		cin >> prod_size;

		Products *newProd.prod_name = new Products[prod_size];
		for(j = 0; j < prod_size; ++j){
			cout << newProd[i].prod_name << "[" << j+1 << "]: ";
			cin >> newProd[i].brand_name[j];
		}
	}
}
Try this:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <string>
#include <iostream>

using std::string; using std::cout;
using std::endl; using std::cin;
using std::cerr;

struct Products
{
  Products()
    : prod_name(), brand_name(), nChildren(0), children(NULL)
  {}

  string prod_name;
  string brand_name;
  unsigned int nChildren;
  Products *children;
};

bool inputprod(unsigned int val, Products* newProd);

int main(int argc, char* argv[])
{
	Products *newProd = NULL;
	if(!inputprod(3, newProd))
		{
		cerr << "Error!!!" << endl;
		}

  return 0;
}

bool inputprod(unsigned int val, Products* newProd)
{
  if (newProd)
    return false;

  newProd = new Products[val]; // dynalloc array of Products of size val

  for (unsigned int i = 0; i < val; ++i)
      {
      cout << "Product" << i+1 << ": ";
      cin >> newProd[i].prod_name;
      cout << "How many child products for " << newProd[i].prod_name << "? ";
      cin >> newProd[i].nChildren;

      newProd[i].children = new Products[newProd[i].nChildren]; // dynalloc array nchildren Products' of current Products
      for(unsigned int j = 0; j < newProd[i].nChildren; ++j)
        {
          cout << newProd[i].prod_name << "[" << j+1 << "]: ";
          newProd[i].children[j].prod_name = newProd[i].prod_name;
          cin >> newProd[i].children[j].brand_name;
        }
    }

  for(unsigned int i = 0; i < val; ++i)
    {
      cout << "Product#" << i << ": " << newProd[i].prod_name << endl;
      for (unsigned int j = 0; j < newProd[i].nChildren; ++j)
          cout << "Child Product#" << j << ": " << newProd[i].children[j].brand_name << endl;
    }

  // tidy up
  for(unsigned int i = 0; i < val; ++i)
	  delete [] newProd[i].children;
  delete [] newProd;
  newProd = NULL;

  return true;
}


Example:


Product1: MyFirstProduct
How many child products for MyFirstProduct? 2
MyFirstProduct[1]: FirstProductChild1
MyFirstProduct[2]: FirstProductChild2
Product2: MySecondProduct
How many child products for MySecondProduct? 1
MySecondProduct[1]: MySecondProductChild1
Product3: MyThirdProduct
How many child products for MyThirdProduct? 0
Product#0: MyFirstProduct
Child Product#0: FirstProductChild1
Child Product#1: FirstProductChild2
Product#1: MySecondProduct
Child Product#0: MySecondProductChild1
Product#2: MyThirdProduct
Thanks, it works now! :D
Topic archived. No new replies allowed.