I thought to resize the CombArray in the void main with the exact number of elements of the total of the combinations before the combs generation (as we do in VB) |
Well first off, you can't have void main. main should always return an int (even though some compilers let you get away with void main, you shouldn't do it).
Secondly, you can't resize arrays. If the array size cannot be determined at compile time (ie: it depends on user input or changes as the program runs), you have two options:
1) dynamically allocate the array with new[] (and remember to free the memory with delete[])
or
2) use a vector (as I tried to show in my previous post)
Vectors are basically resizable arrays. See this reference page:
http://cplusplus.com/reference/stl/vector/
Pretty much, instead of making an array, you'd make a vector:
1 2 3
|
//int CombArray[]; // BAD, doesn't work
vector<int> CombArray; // good
|
A few functions to know about:
- size() gets you the number of elements in the vector, so you don't need to keep track of it seperately
- resize() allows you to explicitly set the size of the array
- push_back() let's you add a new element to the end of the array, increasing the size by 1
- pop_back() drops the last element in the array (decreasing the size by 1)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
vector<int> foo;
foo.resize(3); // make the size of the array 3
foo[0] = 0; // you can then use the [] operator just like a normal array
foo[1] = 1;
foo[2] = 2;
foo.push_back(3); // increase the size by 1, putting the number '3' at the end of the array
cout << foo.size() << "\n"; // prints "4"
// print all elements in the vector:
for(unsigned i = 0; i < foo.size(); ++i)
cout << foo[i];
// prints "0123"
|