Printing Out Arrays In Structs

Sep 16, 2017 at 3:45am
Hey guys so I'm working on a program that requires me to build 3 structs for 3 text files(shipments, customers, orders) containing information for each struct and we have to load in the info via arrays.

I have this so far, but I'm not sure how I'm supposed to print them out and also not sure if my functions are working. But i haven't gotten any errors and i was hoping if there was any way to show the output. I do have to do anyway since I'm supposed to give the user an option to print out records for any of the files, may it be the shipments, customers, or the orders. And when they are displayed, they should be formatted neatly.

How am i supposed to incorporate the formatting? but first of all, how do I print the arrays?

I was thinking of creating a function called
void PrintRecords();
but I'm not sure what to use as parameters and what to write inside to make this work.
Last edited on Sep 17, 2017 at 4:43am
Sep 16, 2017 at 5:46am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

struct shipment{

    std::string FoodItem;
    int ExpDate= 0 ;
    int BoxSize= 0 ;
    int BoxWeight= 0 ;
    int StorageMethod= 0 ;
    int DateReceived= 0 ;
    int ItemPrice= 0 ; // double?
};

// print one shipment
void print_one( const shipment& s ) {

    std::cout << "item: " << s.FoodItem << "  exp date: " << s.ExpDate
              << "  box size: " << s.BoxSize << "  box weight: " << s.BoxWeight
              << "  storage method: " << s.StorageMethod
              << "  date recd:" << s.DateReceived << "  price: " << s.ItemPrice << '\n' ;
}

// print an array of shipments
void print_all( const shipment array[], int num_shipments ) {

    for( int i = 0 ; i < num_shipments ; ++i ) print_one( array[i] ) ;
}
Sep 16, 2017 at 6:02am
Thanks JL . But since im getting my shipment info from a text file, would it affect what u just wrote?
Sep 16, 2017 at 6:06am
No, it does not matter wherefrom you got the contents of the array.
In this case, pass the number of shipments that you have read from the file as the second parameter.
Sep 16, 2017 at 6:06am
Oh wait, i see now that the void print_one is just to print one item. What does the 's' do prior to the dot operator?
Sep 16, 2017 at 6:14am
s is the object (of type shipment); the members of this object are printed out.

With void print_one( const shipment& s ) {,
for the call print_one( array[i] ) ;
the variable s is an alias for the object at array[i]
Sep 16, 2017 at 4:50pm
Okay thanks.

I'm also having an issue in passing by reference with the array of structs.


it gives me an error saying " cannot convert 'orders' to orders* " and so on. What am i missing?
Last edited on Sep 17, 2017 at 4:42am
Sep 16, 2017 at 5:03pm

The error reads : 'no match of 'operator*' (operand type is 'orders')
'no match of 'operator*' (operand type is 'shipments')
'no match of 'operator*' (operand type is 'customers')
Last edited on Sep 17, 2017 at 4:43am
Sep 16, 2017 at 5:36pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ship_info: array (pointer to first element of array)
// n : number of elements in the array 
void getShipments( shipments ship_info[], int n )
{
    ifstream ShipmentInfo("Shipments.txt");

    if( ShipmentInfo.is_open() )
    {
        for( int ship_count = 0; ship_count < n; ++ship_count )
        {
            ShipmentInfo >> ship_info[ship_count].FoodItem
                         >> ship_info[ship_count].ExpDate
                         >> ship_info[ship_count].BoxSize
                         >> ship_info[ship_count].BoxWeight
                         >> ship_info[ship_count].StorageMethod
                         >> ship_info[ship_count].DateReceived
                         >> ship_info[ship_count].ItemPrice;
        }
    }
    // else failed to open file: report error??
}


To call the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintAllRecords()
{
    // ...
    
    const int NUM_SHIPMENTS = 20 ;
    shipments ship_info[NUM_SHIPMENTS] ;

    // pass pointer to first element of array 
    // and the number of elements in the array
    getShipments( ship_info, NUM_SHIPMENTS ) ;
    
    // ...
}
Sep 16, 2017 at 5:55pm
I already declared the the array size of ship_info globally. Or was that wrong too?

and if i take out the asterisks (*ship_info[ship_count]. ..) , it gives me more errors saying that i cannot convert it properly ("cannot convert shipments to shipments**)
Sep 16, 2017 at 7:08pm
I fixed it. But when i print out the stuff, i get a long line of numbers showing instead of the contents in the textfile
Last edited on Sep 17, 2017 at 4:44am
Sep 16, 2017 at 7:57pm
First snippet:
Line 1: Why are you passing in shipment_size? You don't use it. Besides, it is uninitialized in main().

Line 4: What's the point of this line? You don't use it. Remove it.

Second snippet:
Line 7: Why are you declaring a local array here? You want to use the array that was passed in. Delete this line.

Line 9: You're using the shipment info array that was passed in. That's not what you're printing.

Lines 25-31: You're printing the local array which is uninitialized. You want to print the array that was passed in.

You're calling getShipments() twice. Once in main() and again in PrintAllRecords(). You only want to call it once. Delete the call inside PrintAllRecords(). PrintAllRecords should do one thing. i.e. print. An input operation does not belong in a print function.
Topic archived. No new replies allowed.