Array of Structs C++ Program Help

Hello, I am currently working on a C++ program for an array of structs and I am stuck. I have four input files, each one containing different parts of the data for a warehouse inventory. So far, I've managed to input the first two files, sort them using selection sort, then combine them into the first array of structs. I'm supposed to sort them by warehouse name and then within each warehouse sort by item number, but I can't seem to accomplish this.

I am having a little trouble getting the input to go into the correct places in the third and fourth input files. The fourth input file doesn't have a space between the item name and number, so I'm not sure how to approach that as using getline didn't work. Right now the item name and number are being saved into the same string but I need them separate.

Below is my code that I have so far and some examples of the input files. I would really appreciate your help as I have been trying to figure this out for days.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int size = 65;
struct WarehouseInfo
{
    int Item_Number;
    string Item_Name;
    string WareHouse_Name;
    int Quantity;
    float WholesalePrice;
    int MarkUpValue;
    float RetailValue;
    float total;
};
void readIn1(WarehouseInfo STD1[]);
void readIn2(WarehouseInfo STD2[]);
void readIn3(WarehouseInfo STD3[]);
void readIn4(WarehouseInfo STD4[]);
void sort(WarehouseInfo STD1[], WarehouseInfo STD2[], WarehouseInfo STD3[], WarehouseInfo STD4[]);
void print(WarehouseInfo STD1[], WarehouseInfo STD2[], WarehouseInfo STD3[], WarehouseInfo STD4[]);
int main()
{
    WarehouseInfo STD1[size];
    WarehouseInfo STD2[size];
    WarehouseInfo STD3[size];
    WarehouseInfo STD4[size];
    readIn1(STD1);
    readIn2(STD2);
    readIn3(STD3);
    readIn4(STD4);
    sort(STD1, STD2, STD3, STD4);
    print(STD1, STD2, STD3, STD4);
    return 0;
}
void readIn1(WarehouseInfo S1[]) //Read first input file
{
    string ItemName[size];
    ifstream infile;
    infile.open("WLL1.dat");
    while(!infile.eof())
    {
        for(int i = 0; i < size; i++)
        {
            infile >> S1[i].Item_Number;
            getline(infile, ItemName[i]);
        }
    }
    for(int i = 0; i < size; i++)
    {
        S1[i].Item_Name = ItemName[i];
    }
    infile.close();
}
void readIn2(WarehouseInfo S2[]) //Read second input file
{
    string WareHouseName[size];
    ifstream infile;
    infile.open("WLL2.dat");
    while(!infile.eof())
    {
        for(int i = 0; i < size; i++)
        {
            infile >> S2[i].Item_Number;
            getline(infile, WareHouseName[i]);
        }
    }
    for(int i = 0; i < size; i++)
    {
        S2[i].WareHouse_Name = WareHouseName[i];
    }
    infile.close();
}
void readIn3(WarehouseInfo S3[]) //Read third input file
{
    string ItemName[size];
    ifstream infile;
    infile.open("WLL3.dat");
    while(!infile.eof())
    {
        for(int i = 0; i < size; i++)
        {
            getline(infile, ItemName[i], '\t');
            infile >> S3[i].Quantity;
        }
    }
    for(int i = 0; i < size; i++)
    {
        S3[i].Item_Name = ItemName[i];
    }
    infile.close();
}
void readIn4(WarehouseInfo S4[]) //Read fourth input file
{
    string ItemName[size];
    ifstream infile;
    infile.open("WLL4.dat");
    while(!infile.eof())
    {
        for(int i = 0; i < size; i++)
        {
            getline(infile, ItemName[i], '\t');
            infile >> S4[i].Item_Number;
            infile >> S4[i].WholesalePrice;
            infile >> S4[i].MarkUpValue;
        }
    }
    for(int i = 0; i < size; i++)
    {
        S4[i].Item_Name = ItemName[i];
    }
    infile.close();
}
void sort(WarehouseInfo S1[], WarehouseInfo S2[], WarehouseInfo S3[], WarehouseInfo S4[]) //Selection sort
{
    WarehouseInfo temp;
    WarehouseInfo temp2;
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < (size - 1); j++)
        {
            if(S1[j].Item_Number > S1[j+1].Item_Number)
            {
                temp = S1[j];
                S1[j] = S1[j+1];
                S1[j+1] = temp;
            }
            if(S2[j].Item_Number > S2[j+1].Item_Number)
            {
                temp2 = S2[j];
                S2[j] = S2[j+1];
                S2[j+1] = temp2;
            }
        }
    }
    for(int i = 0; i < size; i++)
    {
        S1[i].WareHouse_Name = S2[i].WareHouse_Name;
    }
    return;
}
void print(WarehouseInfo S1[], WarehouseInfo S2[], WarehouseInfo S3[], WarehouseInfo S4[])
{
    for(int i = 0; i < size; i++)
    {
        cout << S1[i].Item_Number << " " << S1[i].Item_Name << " " << S1[i].WareHouse_Name << endl;
    }
    //cout << endl << endl;
    /*for(int i = 0; i < size; i++)
    {
        cout << S3[i].Item_Name << " " << S3[i].Quantity << endl;
    }*/
    /*for(int i = 0; i < size; i++)
    {
        cout << S4[i].Item_Name << " " << S4[i].Item_Number << " " << S4[i].WholesalePrice << " " << S4[i].MarkUpValue << endl;
    }*/
}


Examples of inputs:
Input file 1: contains item number and item name

3310 Galixies-r
1090 Stuffed Stuff-z

Input file 2: contains item number and warehouse name

8100 Swappy South
5504 Mississippi

Input file 3: contains item name and quantity

Whiskers-a 18
HailBock-a 23

Input file 4: contains item nameitem number(no spaces) wholesale price and markup value

Gowns-u2285 24.22 37%
Gowns-i1246 24.84 68%

Thanks
Last edited on
I believe I'm working on this same problem as you and I'm stuck here as well with parsing out the different formats on file 3 and 4.

Did you get anywhere with getline()? Or did you have to go another route?
Topic archived. No new replies allowed.