Retrieve data from struct array

My program uses pointers and structs to read input into an Item struct. If stock of an id no is more than the amount requested, certain bits of info will be put into a sale struct. My issue is that after operation2, there is nothing into the sales[] array.
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  #include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

struct Item{
    int iNo;
    string iName;
    double iPrice;
    int stock;
    int iDiscount;
    int reLevel;
    int reQuan;
};

struct Sale{
    int sNo;
    int quan;
    double total;
    int discount;
    double bill;
};


int searchItem(Item items[], int numItems, int iNo){
    int i;
    i=0;
	while (i<numItems) {
        if(items[i].iNo == iNo)
           return i;
        i=i+1;
    }
    return -1;
}

int searchSale(Sale sales[], int numSales, int iNo){
    int i;
    i=0;
	while (i<numSales) {
        if(sales[i].sNo == iNo)
           return i;
        i=i+1;
    }
    return -1;
}

void Operation1 (Item items[], int numItems, int iNo){
    int found;
    int i;
    Item *m;
    m=(Item*)malloc(sizeof(Item));
            found=searchItem(items, numItems, iNo);
            if (found==-1)
                cout<<"Item "<<iNo<<" was not found."<<endl;
                cout<<endl;
            for(i=0; i<numItems; i=i+1){
            if(items[i].iNo==items[found].iNo){
                cout<<"Displaying part #"<<iNo<<endl;
                cout<<"Part Name: "<< items[i].iName<<endl;
                cout<<"Selling Price: $"<< items[i].iPrice<<endl;
                cout<<"Quantity in Stock: "<< items[i].stock<<endl;
                cout<<"Discount Quantity: "<< items[i].iDiscount<<endl;
                cout<<"Reorder Level: " << items[i].reLevel<<endl;
                cout<<"Reorder Quantity: " << items[i].reQuan << endl;
                cout<<endl;
            }
        }
    }

int Operation2 (Item items[], int numItems, int iNo, int amount){
    int found;
    Sale sales[100];
    Sale *s;
    s=(Sale*) malloc(sizeof(Sale));
    double dis=0.0;
    double itemCost=0.0;
    int numSales=0;
    int re=0;

    found=searchItem(items, numItems, iNo);
        if(found==-1){
            cout<<"Item "<<iNo<<" was not found."<<endl;
            cout<<endl;
        }
        else{
        if(amount<=items[found].stock){
            itemCost=items[found].iPrice*amount;
            (*s).sNo=iNo;
            cout<<amount<<" "<<items[found].iName<<"(s) sold"<<endl;
            cout<<endl;
            items[found].stock=items[found].stock-amount;

        }else{
            cout<<"Not enough stock available."<<endl;
            itemCost=items[found].iPrice;
            cout<<endl;
        }
        if(amount>=items[found].iDiscount){
            dis=(items[found].iPrice*dis);
            itemCost=items[found].iPrice-dis;
            items[found].iPrice=itemCost;
            }
            (*s).quan=amount;
            (*s).total=itemCost;
            (*s).discount=dis;
            (*s).bill=itemCost;
            sales[numSales]=*s;
            numSales=numSales+1;

        }

        return numSales;
}

void Operation3(Sale sales[], Item items[], int numSales, int numItems, int iNo){
    int found;
    int i;
    Sale *s;
    s=(Sale*) malloc(sizeof(Sale));

    found=searchSale(sales, numSales, iNo);

    cout<<"Sales for part #"<<iNo<<endl;
        if(found==-1){
            cout<<"No sales for "<<iNo<<endl;
            cout<<endl;
        }
        else{
        for(i=0; i<numSales; i=i+1){
            if(sales[i].sNo==iNo){
            cout<<"Quantity: "<< sales[i].quan<<endl;
            cout<<"Total Cost: $"<<sales[i].total<<endl;
            cout<<"Discount: $"<< sales[i].discount<<endl;
            cout<<"Bill Amount: $" <<sales[i].bill<<endl;
            cout<<endl;
            }
        }
    }
}

void Operation4(Item items[], int numItems){
    Item *m;
    m=(Item*)malloc(sizeof(Item));
    int re;
    if((*m).stock<(*m).reLevel)
        re=re+1;
    else if (re==0)
        cout<<"There are no items that need reordering."<<endl;
    else{
    if(re>0){
        cout<<"Items that need re-ordering:"<<endl;
        for(int i=0; i<numItems; i=i+1){
            if (items[i].stock<=items[i].reQuan)
                cout<<items[i].iNo<<endl;
        }
    }
}
}

int main(){
    ifstream in;
    ifstream fin;
    ofstream out;

    Item items[100];
    Item *m;
    m=(Item*)malloc(sizeof(Item));

    Sale sales[100];
    Sale *s;
    s=(Sale*) malloc(sizeof(Sale));
    int numItems=0;
    int id;
    int i=0;
    in.open("input.txt");
        in>>id;
        while(id!=-1){
            (*m).iNo = id;
            in>> (*m).iName >> (*m).iPrice >>  (*m).stock >> (*m).iDiscount >> (*m).reLevel >> (*m).reQuan;

            items[i] = *m;
            numItems = numItems + 1;
            i=i+1;
            in >> id;
    }
    in.close();

    int re=0;
    int numSales=0;
    int iNo;
    int op;
    int amount;
    if(numItems==-1)
        return 0;

    in.open("sales.txt");
        in>>op;
    while(op!=5){
        in>>iNo;
        in>>amount;

    if(op==1)
        Operation1(items, numItems, iNo);

    if(op==2){
    numSales=Operation2(items, numItems, iNo, amount);
    cout<<endl;
    }

    if(op==3)
    Operation3(sales, items, numSales, numItems, iNo);

    if(op==4)
    Operation4(items, numItems);

        in>>op;
    }
    if(op==5)
    return 0;

}

What is the purpose of malloc?
It allocates storage for a struct
example) A sale is stored in memory using a struct, created using malloc.
Yes ...

its here, with three others. Who are they? Do they differ?
1
2
3
4
5
6
7
Item *m = (Item*)malloc(sizeof(Item));

Item *x = new Item;

Item y;

auto z = std::make_unique<Item>();



That aside, how do you see that 'sales' is empty after the 'Operation2'?

(Btw, how does name OperationX describe what the function does? Should it?)
Operation1 is followed by an item number. A search is made for the item with that number. If it is found, its information is written to the output file. If it is not found, an appropriate error message is written to the file.

Operation2 is followed by an item number and amount. If the item is present, a check is made to see if the amount is less than or equal to the quantity in stock. If so, a sale struct is created in memory and its address is added to the array of pointers to sales structs. The quantity in stock of the item is also updated. Note that if the amount is greater than or equal to the discount quantity of the item, a discount of 10% is given. If the item is not present or there are not enough items for the sale, an error message must be written to the output file. If the sale is successful, an appropriate message is written to the output file.

Operation3 is followed by an item number. If the item number is found, all the sales for that item are written to the output file; otherwise, an error message is written to the output file. The sales are found by traversing the array of pointers to sales structs.

Operation4 is NOT followed by any further input on that line. A list of all the items to be reordered must be written to output (item number alone sufficient), together with the reorder amount.
I have to guess that this is your answer to one question:
The sales are found by traversing the array of pointers to sales structs.


Where is this "array of pointers to sales structs"?


Can you tell the difference between m, x, y, and z in my previous post?
If were to output everything in the sales array at the end of Operation2,
for eg)
Sample Input:
111 hammer 50.00 100 20 30 50 //ID, name, etc
121 screwdriver 1.25 1000 100 500 400
151 drill 69.99 10 2 5 5
-1

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
int Operation2 (Item items[], int numItems, int iNo, int amount){
    int found;
    Sale sales[100];
    Sale *s;
    s=(Sale*) malloc(sizeof(Sale));
    double dis=0.0;
    double itemCost=0.0;
    int numSales=0;
    int re=0;

    found=searchItem(items, numItems, iNo);
        if(found==-1){
            cout<<"Item "<<iNo<<" was not found."<<endl;
            cout<<endl;
        }
        else{
        if(amount<=items[found].stock){
            itemCost=items[found].iPrice*amount;
            (*s).sNo=iNo;
            cout<<amount<<" "<<items[found].iName<<"(s) sold"<<endl;
            cout<<endl;
            items[found].stock=items[found].stock-amount;

        }else{
            cout<<"Not enough stock available."<<endl;
            itemCost=items[found].iPrice;
            cout<<endl;
        }
        if(amount>=items[found].iDiscount){
            dis=(items[found].iPrice*dis);
            itemCost=items[found].iPrice-dis;
            items[found].iPrice=itemCost;
            }
            (*s).quan=amount;
            (*s).total=itemCost;
            (*s).discount=dis;
            (*s).bill=itemCost;
            sales[numSales]=*s;
            numSales=numSales+1;

            for(i=0; i<numSales; i=i+1){
            out<<"Quantity: "<< sales[i].quan<<endl;
            out<<"Total Cost: $"<<sales[i].total<<endl;
            out<<"Discount: $"<< sales[i].discount<<endl;
            out<<"Bill Amount: $" <<sales[i].bill<<endl;
            out<<endl;
            }
    }

I would get:


1 hammer(s) sold

Quantity: 1
Total Cost: $50
Discount: $0
Bill Amount: $50


5 hammer(s) sold

Quantity: 5
Total Cost: $250
Discount: $0
Bill Amount: $250


but if I were to print it in Operation3, nothing shows
Once more:
Where is the "array of pointers to sales structs"?
Topic archived. No new replies allowed.