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; } }



LinkBack URL
About LinkBacks



