# Thread: Dynamic Array of Structs help

1. ## Dynamic Array of Structs help

Hi, I am new here first off.

Second, I need help debugging code. This program asks the user for the number of boxes. Then, it creates an array of box structs equal to the number specified by the user. Next, the user enters the relevant information.

The problem is after going through the loop for the first time and entering information, the program runs into an error. The user types in the name of the second box, then the program crashes.

Code:
```// Chapter 7, Exercise 3

#include <iostream>

struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};

void set_box(box * bx[], int n);      // prototype
void display_box(box bx[], int n);    // prototype

int main() {
using namespace std;
int num_boxes = 0;

// prompt user for number of boxes
cout << "How many boxes: ";
cin >> num_boxes;
// get rid of newline
cin.get();

// dynamically create an array of box structs
box * ptr = new box [num_boxes];

// set the parameters for each box
set_box(&ptr, num_boxes);

// display parameters for each box
display_box(ptr, num_boxes);

// end of program
delete [] ptr;
cout << "\nDone\n";
system("pause");
return(0);
}

void set_box(box * bx[], int n) {
using namespace std;
for(int i = 0; i < n; i++) {
cout << "Enter the maker's name: ";
cin.getline(bx[i]->maker, 40);
cout << "Enter box height: ";
cin >> bx[i]->height;
cout << "Enter box width: ";
cin >> bx[i]->width;
cout << "Enter box length: ";
cin >> bx[i]->length;
cin.get();
bx[i]->volume = bx[i]->height * bx[i]->width * bx[i]->length; }
}

void display_box(box bx[], int n) {
using namespace std;
for(int i = 0; i < n; i++) {
cout << "Maker: " << bx[i].maker << endl;
cout << "Height: " << bx[i].height << endl;
cout << "Width: " << bx[i].width << endl;
cout << "Length: " << bx[i].length << endl;
cout << "Volume: " << bx[i].volume << endl;
cout << endl; }
}```

2. When you pass in an array, choose either * or [], not both.

3. Originally Posted by tabstop
When you pass in an array, choose either * or [], not both.
The thing that gets me is that the programs works the first time trough the loop (let us say the number of boxes is 2).

4. Thanks for your help. I realize that when I was passing in a pointer-pointer into a function that expects a pointer.

The line: box * ptr = new box [num_boxes];

I forgot that ptr was already a pointer, so to pass in the address I just needed to pass in ptr not &ptr.

Code:
```// Chapter 7, Exercise 3

#include <iostream>

struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};

void set_box(box * bx, int n);      // prototype
void display_box(box bx[], int n);    // prototype

int main() {
using namespace std;
int num_boxes = 0;

// prompt user for number of boxes
cout << "How many boxes: ";
cin >> num_boxes;
// get rid of newline
cin.get();

// dynamically create an array of box structs
box * ptr = new box [num_boxes];

// set the parameters for each box
set_box(ptr, num_boxes);

// display parameters for each box
display_box(ptr, num_boxes);

// end of program
delete [] ptr;
cout << "\nDone\n";
system("pause");
return(0);
}

void set_box(box * bx, int n) {
using namespace std;
for(int i = 0; i < n; i++) {
cout << "Enter the maker's name: ";
cin.getline(bx[i].maker, 40);
cout << "Enter box height: ";
cin >> bx[i].height;
cout << "Enter box width: ";
cin >> bx[i].width;
cout << "Enter box length: ";
cin >> bx[i].length;
cin.get();
bx[i].volume = bx[i].height * bx[i].width * bx[i].length; }
}

void display_box(box bx[], int n) {
using namespace std;
for(int i = 0; i < n; i++) {
cout << "Maker: " << bx[i].maker << endl;
cout << "Height: " << bx[i].height << endl;
cout << "Width: " << bx[i].width << endl;
cout << "Length: " << bx[i].length << endl;
cout << "Volume: " << bx[i].volume << endl;
cout << endl; }
}```

5. Ok, I think my explaination was wrong, but it works...

6. Originally Posted by fairguynova
Ok, I think my explaination was wrong, but it works...
If you are talking about the "ptr was already a pointer" part, then your explanation does make sense to me.

Instead of blindly accepting the user's input, you might want to perform some input validation and request for re-entry for invalid input. For example, what happens if the user enters a negative number for the number of boxes? What happens if the user enters alphabetic input?

Along the same lines, what happens if the user types in the number and then a space? The space would be consumed by the cin.get(), but the newline would be left on the input buffer. One way to avoid this is to ignore whatever is left on the buffer up to and including the newline instead of just using cin.get(), e.g.,
Code:
`cin.ignore(numeric_limits<streamsize>::max(), '\n');`
You would need to #include <limits> for std::numeric_limits, but you could also write some arbitrarily "large" number, e.g.,
Code:
`cin.ignore(1000, '\n');`
Oh, and if you are not required to manage the memory of the dynamic array yourself, #include <vector> and use a std::vector<box> instead.