Comparing integers involving member functions & substructs

Hello all. I'm not getting any errors in my code, but I'm trying to do a bubble sort to sort shipment delivery dates in ascending order. Here's what I have:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106


struct Date {

    int month;
    int day;
    int year;
};

struct Measurements {
    int num1;
    int num2;
    int num3;
};

struct shipments {

    string FoodName;
    Date ExpireyDate;
    Measurements BoxSize;
    int BoxWeight;
    string StorageMethod;
    Date ReceiveDate;
    string ItemPrice;

  
    int ShipmentsProcessing(int length);

};

void getShipments(shipments shipInfo[], int shipmentSize, int shipmentcount);
void getOrders(orders[], int);


int main()
{
    int length;
    shipments ShipStuff;


   ShipStuff.ShipmentsProcessing(length);

   return 0;
}

// Implementation of functions and member functions

void getShipments(shipments shipInfo[], int shipmentSize, int shipmentcount)
{
    shipmentSize = 100;
    shipInfo[shipmentSize];
    char character;

    ifstream Shipments;
    Shipments.open("Shipments.txt");

        if (!Shipments.fail())
        {

            for (shipmentcount = 0; shipmentcount < shipmentSize; shipmentcount++)
            {
                Shipments >> shipInfo[shipmentcount].FoodName
                          >> shipInfo[shipmentcount].ExpireyDate.month >> character >> shipInfo[shipmentcount].ExpireyDate.day >> character >> shipInfo[shipmentcount].ExpireyDate.year
                          >> shipInfo[shipmentcount].BoxSize.num1>> character >> shipInfo[shipmentcount].BoxSize.num2 >> character >> shipInfo[shipmentcount].BoxSize.num3
                          >> shipInfo[shipmentcount].BoxWeight
                          >> shipInfo[shipmentcount].StorageMethod
                          >> shipInfo[shipmentcount].ReceiveDate.month>> character >>  shipInfo[shipmentcount].ReceiveDate.day >> character >>  shipInfo[shipmentcount].ReceiveDate.year
                          >> shipInfo[shipmentcount].ItemPrice;
            }
        }
    else
    {
        cout<<"Sorry but the file 'Shipments.txt' is invalid."<<endl;
    }

}

int shipments::ShipmentsProcessing(int length)
{
     int arrSize = 100;
    shipments shipInfo[arrSize];
    shipments temp;
    int iteration;
    int index;

    for (iteration = 1; iteration < length; iteration++)
    {

        for(index = 0 ; index < length - iteration; index++)
            if(shipInfo[index].ExpireyDate.year >  shipInfo[index + 1].ExpireyDate.year)
        {
            temp = shipInfo[index];
            shipInfo[index] = shipInfo[index + 1 ];
            shipInfo[index + 1] = temp;
        }
    }
    // Initially bubble sort through array pertaining in ascending order through received date:


return 0;


}




i don't get any errors but everytime I run it, the program crashes. What is there to fix?
Last edited on
The textfile I'm reading off of has this kind of format

1) Name of item
2) Exp. Date
3) Box size in inches
4) Box Weight
5) Storage method (R)
6) Date Received
7) item price


For this one im jst looking at the date received:
Donuts 01:30:2015 10:11:12 20 R 01:21:2015 12.00

in this instance it would be 01:21:2015
Last edited on
I expect you're trying to read or write an array element that doesn't exist.

For example, if you have an array of length 5, and you try to read array[9999999] it'll often crash.

You need to identify where the crash is happening. Do this by adding output in lots of places so you know which code has been executed.

Then, when you know which line is crashing, you can identify what the bad value on that line is.

While I'm here, this: shipInfo[shipmentSize]; makes no sense at all. None at all. What are you trying to do with it?
@Repeater

Thanks for responding swiftly. And sorry, i forgot to remove that. At that line it wouldn't run without placing the size in the [] so i made that bit. Should i replace shipmentSize with shipmentcount instead?
Wait no replacing shipmentiSize to shipmentcount wouldn't do anything but make more errors. There are 100 items in the text file that im trying to read into the struct shipments btw
shipInfo[shipmentSize];
That simply doesn't do anything.

It's like writing this in your code:
x;
See how this code does nothing? That's like shipInfo[shipmentSize];. Doesn't do anything.

Doesn't do anything. When you wrote shipInfo[shipmentSize];, what did you thikn it would do? Are you trying to create a variable? Are you trying to read sometihng? Are you trying to write something? It doesn't do anything. What are you trying to do?
Hints:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <array>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>

struct Date {
    int month {};
    int day {};
    int year {};
    friend bool operator<(const Date& lhs, const Date& rhs)
    {
        if(lhs.year < rhs.year)   { return true; }
        if(lhs.year > rhs.year)   { return false; }
        if(lhs.month < rhs.month) { return true; }
        if(lhs.month > rhs.month) { return false; }
        if(lhs.day < rhs.day)     { return true; }
        else                      { return false; }
    }
    friend std::istream& operator>>(std::istream& is, Date& rhs)
    {
        char c {};
        is >> rhs.month >> c >> rhs.day >> c >> rhs.year;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const Date& rhs)
    {
        return os << rhs.month << '/' << rhs.day << '/' << rhs.year;
    }
};

struct Measurements {
    int num1 {};
    int num2 {};
    int num3 {};
    friend std::istream& operator>>(std::istream& is, Measurements& rhs)
    {
        char c {};
        is >> rhs.num1 >> c >> rhs.num2 >> c >> rhs.num3;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const Measurements& rhs)
    {
        return os << rhs.num1 << 'x' << rhs.num2 << 'x' << rhs.num3;
    }
};

struct Shipment {
    std::string name;
    Date expiry;
    Measurements sides;
    int weight {};
    std::string storage_method;
    Date receive_date;
    double item_price {};
    friend bool operator<(const Shipment& lhs, const Shipment& rhs)
    {
        return lhs.receive_date < rhs.receive_date;
    }
    friend std::istream& operator>>(std::istream& is, Shipment& rhs)
    {
        is >> rhs.name >> rhs.expiry >> rhs.sides >> rhs.weight 
           >> rhs.storage_method >> rhs.receive_date >> rhs.item_price;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const Shipment& rhs)
    {
        os << rhs.name << ": expiry date: " << rhs.expiry 
           << "; box: " << rhs.sides 
           << "; weight: " << rhs.weight 
           << "; st. method: " << rhs.storage_method 
           << "; received: " << rhs.receive_date 
           << "; price: $" << std::fixed << std::setprecision(2)
           << rhs.item_price;
        return os;
    }
};

int readShipments(const std::string& filename, Shipment*& shipinfo);
unsigned readFileNumLines(std::ifstream& file);
int readDataIntoArray(std::ifstream& file, Shipment* shipinfo);
bool sortShipments(Shipment*& shipinfo, size_t howmany);
void waitForEnter();

int main()
{
    Shipment* ship_stuff;
    size_t ship_stuff_size {};
    {
        int check = readShipments("Shipments.txt", ship_stuff);
        if(check < 0) { // there must be some errors
            if(check == -1) { /* file error - exit */ }
            else            { /* unmanaged error - exit */}
        } else if(check == 0) { /* empty file - exit */
        } else /* 0 < check */{ ship_stuff_size = static_cast<size_t>(check); }
    } // 'check' goes out of scope here
    std::cout << "Before sorting:\n";
    for(size_t i {}; i<ship_stuff_size; ++i) { 
        std::cout << ship_stuff[i] << '\n';
    }
    sortShipments(ship_stuff, ship_stuff_size);
    std::cout << "\nAfter sorting:\n";
    for(size_t i {}; i<ship_stuff_size; ++i) { 
        std::cout << ship_stuff[i] << '\n';
    }
    waitForEnter();
    return 0;
}

// Read data from the file 'filename' into 'shipinfo'.
// Returns: 'shipinfo' new size if file read (it could be different from number
//           of lines actually read);
//          -1 if file can't be opened;
//          0 if file empty
int readShipments(const std::string& filename, Shipment*& shipinfo)
{
    std::ifstream in(filename);
    if(!in) {
        std::cout << "Can't open " << filename << '\n';
        return -1;
    }
    unsigned numlines = readFileNumLines(in);
    if(!numlines) {
        std::cout << "No data to be read inside " << filename << '\n';
        return 0;
    }

    // Allocate enough memory for all the lines:
    shipinfo = new Shipment[numlines] {};
    readDataIntoArray(in, shipinfo);
    in.close();
    return numlines;
}

unsigned readFileNumLines(std::ifstream& file)
{
    file.seekg(0, std::ios::beg);
    unsigned count {};
    for(std::string line; std::getline(file, line); ++count) {}
    file.clear(); // restore file form EOF
    file.seekg(0, std::ios::beg);
    return count;
}

int readDataIntoArray(std::ifstream& file, Shipment* shipinfo)
{
    int i {};
    for(std::string line; std::getline(file, line); ++i) {
        std::istringstream ss(line);
        // File format:
        // 1) Name of item
        // 2) Expiry date
        // 3) Box size in inches
        // 4) Box weight
        // 5) Storage method (R)
        // 6) Date received
        // 7) Item price
        // Ex. Donuts 01:30:2015 10:11:12  20   R   01:21:2015 12.00
        //      name    expiry     sides  weig  sm   received  price
        ss >> shipinfo[i];
    }
    return i;
}

bool sortShipments(Shipment*& shipinfo, size_t howmany)
{
    if(howmany == 0) { return false; }
    bool flag { true };
    for(size_t i {1}; i<howmany && flag; ++i) {
        flag = false;
        for(size_t j {}; j<(howmany-1); ++j) {
            if(shipinfo[j] < shipinfo[j+1]) {
                // We currently rely on default copy assignment
                Shipment temp = shipinfo[j];
                shipinfo[j] = shipinfo[j+1];
                shipinfo[j+1] = temp;
                flag = true;
            }
        }
    }
    return true;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Shipments.txt:
Donuts 01:30:2015 10:11:12 20 R 01:21:2015 12.00
Leeks 09:13:2017 10:20:30 14 R 11:04:2016 8.00
Lemons 04:06:2017 15:25:45 36 R 04:07:2017 2.50


Output:
Before sorting:
Donuts: expiry date: 1/30/2015; box: 10x11x12; weight: 20; st. method: R; received: 1/21/2015; price: $12.00
Leeks: expiry date: 9/13/2017; box: 10x20x30; weight: 14; st. method: R; received: 11/4/2016; price: $8.00
Lemons: expiry date: 4/6/2017; box: 15x25x45; weight: 36; st. method: R; received: 4/7/2017; price: $2.50

After sorting:
Lemons: expiry date: 4/6/2017; box: 15x25x45; weight: 36; st. method: R; received: 4/7/2017; price: $2.50
Leeks: expiry date: 9/13/2017; box: 10x20x30; weight: 14; st. method: R; received: 11/4/2016; price: $8.00
Donuts: expiry date: 1/30/2015; box: 10x11x12; weight: 20; st. method: R; received: 1/21/2015; price: $12.00

Press ENTER to continue...

Topic archived. No new replies allowed.