Thread: structs and arrays

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Angry structs and arrays

    in my program i have a struct called Packages which contains the members weight, length, width and depth. This data is in a file whose first line is the number of packages, and then the lines after are the package information, each package having its own line. I have to use one function to read in the number of packages, allocate the correct number of Package struct elements in the array and then have a loop which calls another function that is used to get the information for a single package, so it loops untill i have each package in the array. I am very confused on how to do this.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is an example of how I addressed using an array of structs awhile back.. this is very similar to what you want to do. Take a look and see if you can get the jist of what is going on.
    Last edited by The Brain; 01-25-2005 at 02:49 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    head ache

    I can't really make the connection between that example and the one im am faced with. I have a function getPkgs that first gets the number of packages from the first line of the data file. next this function contains a for loop that will call a second function inPack that will get the data for a single package each time the loop goes through until all the packages are inputted. Im not sure how to write the second function or how to get the information from the file into the array. I have and array packList[] and a struct

    struct Package{
    float weight;
    float length;
    float width;
    float depth;
    };

    If you can help me get this sorted out I'd appreciate it

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you already have a function that gets the number of packages from your file, what's the difference between getting that value and getting these other values? You're just reading in floats this time instead of what was most likely an integer value.

    Is this a binary file or text file?

    Assuming text data (whitespace delimited), you can use the stream extractor operator >> just like you might have (probably?) used to get the first value (the count). It sounds, based on your description of how you want the second function to work, that the function probably just needs a single line of code similar to:

    Code:
    name_of_input_stream >> whatever.weight >> whatever.length >> whatever.width >> whatever.depth;
    If binary data you're going to need to use the stream's read member function.

    You are probably going to need to pass in the stream object (by reference) as an argument to the function, unless your stream object happens to be global (which it should not be); you're also going to need to pass the array element to be filled, a pointer/reference to a single Package element and what I have refered to as whatever in the above sample.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here are a couple of tips:


    1) in the main body of your program, instantiate an array of stuct objects

    Code:
    Package myPackages[40];

    2) your void inPack(Packages& myPackages[], int current_index); function will probaby exhibit this type of behavior:
    Code:
    cout << "Enter weight: ";
    cin >> myPackages[current_index].weight;
    
    cout << "Enter Length: ";
    cin >> myPackages[current_index].length;
    
    
    etc...
    etc...
    }



    edit: ^^^^^ what hk_mp5kpdw said is probably a more efficient method ^^^^^^^
    Last edited by The Brain; 01-26-2005 at 10:00 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    thanks for the help i think i got it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  4. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  5. dynamic arrays of structs...
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 12-14-2002, 06:57 AM