Dynamic Array of Structs help

This is a discussion on Dynamic Array of Structs help within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new here first off. Second, I need help debugging code. This program asks the user for the ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    14

    Exclamation 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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When you pass in an array, choose either * or [], not both.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Quote Originally Posted by tabstop View Post
    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. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    14

    Smile

    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. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Ok, I think my explaination was wrong, but it works...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  2. dynamic array of vectors
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2006, 11:01 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-03-2005, 11:13 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21