Just will not work!

Pages: 12
Can't figure out why this will not output report.

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
#include <fstream>  
#include <iostream>  
#include <iomanip>  
#include <vector>  
#include <string>  
using namespace std;  
const int invSize = 2;  
void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  
void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);   
void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  
void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice); 
void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  
int main()  
{  
vector<int> itemQuantity;  
vector<string> itemID;  
vector<string> itemName;  
vector<int> pOrdered;  
vector<int> pInStore;  
vector<int> pSold;  
vector<double> manufPrice;  
vector<double> sellingPrice;  
int itemNumber;  
ifstream infile;  
infile.open("C:\\inventory.txt");  
if (!infile)  
{  
cout << "C:\\inventory.txt not read." << endl;  
system("pause");
return 1;  
}  
getInventory(infile, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  
displayMenu(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  
infile.close();  
return 0;  
}  
void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice)  
{  
unsigned int i;  
string line;  
for (i = 0; i < itemID.size(); i++)  
{  
infile >> itemID[i]   
>> itemName[i]   
>> pOrdered[i]   
>> manufPrice[i]   
>> sellingPrice[i];  
pInStore[i] = pOrdered[i];  
}
pSold[0] = 0;  
}  
void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice) 
{  
char outputResponce;  
cout << "Type 'C' to check whether or not an item is in stock." << endl;  
cout << "Type 'S' to sell an item to a customer." << endl;  
cout << "Type 'R' to print an inventory report." << endl;     
cin >> outputResponce;  
if (outputResponce == 'C' ||outputResponce == 'c')  
itemSearch(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  
if (outputResponce == 'S' ||outputResponce == 's')  
sellItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  
if (outputResponce == 'R' ||outputResponce == 'r')  
printReport(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  
}  
void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice)  
{  
int amtBought;  
double cash;  
char verifyItem;  
int i;  
int itemNumber;  
for (i = 0; i < 5; i++)  
{  
cout << "What is being purchased? Input item number. ";  
cin >> itemNumber;                                     
i = itemNumber;  
if (i != -1)  
{  
cout << "Selling " << itemName[i] << ", at a costs of $" << sellingPrice[i]  
<< ".  OK ? (Y/N)" << endl;  
cout << endl;  
cin >> verifyItem;  
if (verifyItem == 'N' || verifyItem == 'n')  
{  
cout << "Enter what is being sold by item number? " << endl;  
cin >> itemNumber;  
}  
cout << "How many items are required? " << endl;  
cin >> amtBought;  
if (amtBought < 1)  
cout << "Number purchased!" << endl;  
else 
{  
if (pInStore[i] < amtBought)  
cout << "Item not available" << pInStore[i] << " can be sold." << endl;  
else 
{  
cout << "Total" << amtBought * sellingPrice[i] << endl;  
cout << "Funds tendered" << endl;  
cin >> cash;  
if (cash < (amtBought * sellingPrice[i]))  
{  
cout << "Insufficient funds tendered." << endl;  
continue;  
}  
else 
{  
pInStore[i] = pInStore[i] - amtBought;  
cout << "Return" << cash - (amtBought * sellingPrice[i]) << "change." << endl;  
}  
}  
}  
}  
}  
}  
void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice)  
{  
int number;  
cout << "Which would you like to check?" << endl;  
cout << endl;  
cin >> number;  
if (pInStore[number] > 0)  
cout << "Currently you have " << pInStore[number] << "of that item in house." << endl;  
}  
void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,    
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice)  
{  
unsigned int i;  
int totalItems = 0;  
double totalInventory = 0;  
cout << "Friendly Hardware Store" << endl << endl;  
cout << "itemID ItemName pOrdered pInStore pSold manufPrice sellingPrice" << endl;  
cout << fixed << showpoint;  
cout << setprecision(2);  
for (i = 0; i < itemID.size(); i++)  
{  
cout << left;  
cout << setw(15) << itemID.at(i);  
cout << setw(15) << itemName.at(i);  
cout << right;  
cout << setw(15) << pOrdered.at(i);  
cout << setw(15) << pInStore.at(i);  
cout << setw(15) << pSold.at(i);  
cout << setw(15) << manufPrice.at(i);  
cout << setw(15) << sellingPrice.at(i) << endl;  
totalInventory += pInStore.at(i) * sellingPrice.at(i);  
totalItems += pInStore.at(i);  
}  
cout << endl;  
cout << "Total Inventory: $" << totalInventory << endl;  
cout << "Total number of items in the store: " << totalItems << endl;  
}
How can you even begin to read that program without any indentation?
https://en.wikipedia.org/wiki/Indent_style

Have you considered using a vector of a structure or class that holds all the fields of each inventory item instead of all of those parallel vectors?




How is the format of the file ?
The parameters of the exercise force me to use parallel vectors. As far as reading it goes, I just find this way easier sorry. I will work on doing the indentation. The input file holds this data:
itemID itemName pOrdered pInStore pSold manufPrice sellingPrice
4444 Circular Saw 150 150 40 45.00 125.00
3333 Cooking Range 50 50 20 450.00 850.00

I hope this helps because I am having a bit of a time with the input file anyway. I am beginning to think this is the problem. This is my first shot at a program that uses an input file so I don't know for sure how it is messed up. Sorry so lame.
My best attempt
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <fstream>  
#include <iostream>  
#include <iomanip>  
#include <vector>  
#include <string>  

using namespace std;  

const int invSize = 2;  

void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, 
vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  

void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);   

void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  

void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice); 

void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   
vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   
vector<double> sellingPrice);  

int main()  

{  

vector<int> itemQuantity;  

vector<string> itemID;  

vector<string> itemName;  

vector<int> pOrdered;  

vector<int> pInStore;  

vector<int> pSold;  

vector<double> manufPrice;  

vector<double> sellingPrice;  

int itemNumber;  

ifstream infile;  

infile.open("C:\\inventory.txt");  

if (!infile)  

    {  

     cout << "C:\\inventory.txt not read." << endl;  

     system("pause");

     return 1;  

    }  

getInventory(infile, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  

displayMenu(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  

infile.close();  

return 0;  

}  

void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   

vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   

vector<double> sellingPrice)  

{  

unsigned int i;  

string line;  

for (i = 0; i < itemID.size(); i++)  
   
    {  
    
     infile >> itemID[i]   
   
     >> itemName[i]   
    
     >> pOrdered[i]   
   
     >> manufPrice[i]   
    
     >> sellingPrice[i];  
  
     pInStore[i] = pOrdered[i];  
    
    }

pSold[0] = 0;  

}  

void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   

vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   

vector<double> sellingPrice) 

{  

char outputResponce;  

cout << "Type 'C' to check if item is in stock." << endl;  

cout << "Type 'S' to sell an item." << endl;  

cout << "Type 'R' to print report." << endl;     

cin >> outputResponce;  

if (outputResponce == 'C' ||outputResponce == 'c')  

itemSearch(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  

if (outputResponce == 'S' ||outputResponce == 's')  

sellItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  

if (outputResponce == 'R' ||outputResponce == 'r')  

printReport(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);  

}  

void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   

vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   

vector<double> sellingPrice)  

{  

int numPurch;  

double funds;  

char verify;  

int i;  

int itemNumber;  

for (i = 0; i < 5; i++)  

    {  

     cout << "Enter the item number of salls item. ";  

     cin >> itemNumber;                                     

     i = itemNumber;  

     if (i != -1)  

         {  

          cout << "Selling " << itemName[i] << ", at a costs of $" << sellingPrice[i]  

          << ".  OK ? (Y/N)" << endl;  

          cout << endl;  

          cin >> verify;  

          if (verify == 'N' || verify == 'n')  

            {  

             cout << "Enter what is being sold by item number? " << endl;  

             cin >> itemNumber;  

            }  

cout << "How many items are required? " << endl;  

cin >> numPurch;  

if (numPurch < 1)  

cout << "Number purchased!" << endl;  

else 

            {  

             if (pInStore[i] < numPurch)  

             cout << "Item not available" << pInStore[i] << " can be sold." << endl;  

             else 

                {  

                 cout << "Total" << numPurch * sellingPrice[i] << endl;  

                 cout << "Funds tendered" << endl;  

                 cin >> funds;  

                 if (funds < (numPurch * sellingPrice[i]))  

                    {  

                     cout << "Insufficient funds tendered." << endl;  

                     continue;  

                    }  

                     else 

                        {  

                         pInStore[i] = pInStore[i] - numPurch;  

                         cout << "Return" << funds - (numPurch * sellingPrice[i]) << "change." << endl;  

                         }  

                    }  

                 }  

              }  

           }  

        }  

   void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,   

   vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   

   vector<double> sellingPrice)  

       {  

        int number;  

        cout << "Which would you like to check?" << endl;  

        cout << endl;  

        cin >> number;  

        if (pInStore[number] > 0)  

        cout << "Currently you have " << pInStore[number] << "of that item in house." << endl;  

        }  

void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,    

vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,   

vector<double> sellingPrice)  

       {  

        unsigned int i;  

        int totalItems = 0;  

        double totalInventory = 0;  

        cout << "Friendly Hardware Store" << endl << endl;  

        cout << "itemID ItemName pOrdered pInStore pSold manufPrice sellingPrice" << endl;  

        cout << fixed << showpoint;  

        cout << setprecision(2);  

        for (i = 0; i < itemID.size(); i++)  

            {  

             cout << left;  

             cout << setw(15) << itemID.at(i);  

             cout << setw(15) << itemName.at(i);  

             cout << right;  

             cout << setw(15) << pOrdered.at(i);  

             cout << setw(15) << pInStore.at(i);  

             cout << setw(15) << pSold.at(i);  

             cout << setw(15) << manufPrice.at(i);  

             cout << setw(15) << sellingPrice.at(i) << endl;  

             totalInventory += pInStore.at(i) * sellingPrice.at(i);  

             totalItems += pInStore.at(i);  

            }  

        cout << endl;  

        cout << "Total Inventory: $" << totalInventory << endl;  

        cout << "Total number of items in the store: " << totalItems << endl;  

      }



I ran your program and found the first problem at the getInventory function.
All the vectors you pass to this function are empty and so nothing is read at your for loop.
At the end you try to access and empty vector pSold[0] = 0;
Try to fix this first.
The problem with your input file is that the "name" field can contain spaces. For example, given this input line:
4444 Circular Saw 150 150 40 45.00 125.00 

And this code:
1
2
3
4
5
     infile >> itemID[i]   
     >> itemName[i]   
     >> pOrdered[i]   
     >> manufPrice[i]   
     >> sellingPrice[i];  

The program will read "4444" into itemID[i] and then it will read "Circular" into itemName[i].
Next it tries to read "Saw" into pOrdered[i], but since pOrdered[i] is an int, the code fails.

There are some other problems with getInventory:
1. The loop won't execute as Thomas1965 said because upon entry, itemId.size() is zero.
2. If a line in the input is missing a field, you run the risk of having the program think that a record spans across two lines in the input. That would be a real problem to find.
3. The vectors need to be passed by reference. Actually you should pass the vectors by reference or const reference to ALL of your functions.
4. You say that
The input file holds this data:
itemID itemName pOrdered pInStore pSold manufPrice sellingPrice
4444 Circular Saw 150 150 40 45.00 125.00
3333 Cooking Range 50 50 20 450.00 850.00

but getInventory sets pInStore based on the number ordered and it sets sold to zero. So should pInStore and pSold be in the input file at all?

Here is a version of getInventory that assumes inventory.txt contains:
itemID pOrdered manufPrice sellingPrice itemName
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
void
getInventory(ifstream & infile, vector < string > &itemID, vector < string > &itemName,
             vector < int > &pOrdered, vector < int > &pInStore, vector < int > &pSold,
             vector < double > &manufPrice, vector < double > &sellingPrice)
{
    string id, name;
    int ordered;
    double mprice, sprice;
    unsigned lineNo=1;
    string line;
    while (getline(infile, line)) {
        istringstream is(line);
        // Now extract the data from the line. We use getline() to read the name
        // because it can contain spaces.
        if ((is >> id >> ordered >> mprice >> sprice) && getline(is,name)) {
            // It's a good record, append the data to the vectors
            itemID.push_back(id);
            itemName.push_back(name);
            pOrdered.push_back(ordered);
            manufPrice.push_back(mprice);
            sellingPrice.push_back(sprice);
            pInStore.push_back(ordered);
            pSold.push_back(0);
        } else {
            cout << "Invalid input at line " << lineNo << ":\n";
            cout << line;
        }
        ++lineNo;
    }
}


This is hopeless, going back to ground zero.

Thanks all.
You're actually pretty close to working code. It's messy because you have to use separate vectors for each field. I'd keep going with the code that you have.
greengirl1968,

Don't give up yet, as dhayden says you are close. I just got it working a little while ago and it finally worked when I passed the vectors by reference. Although my read file solution is not as nice as dhayden's it solved the input problem for now until I come to understand how his works.

keep asking questions,

Andy
Alright, I will give it another shot. Still getting error compiling with dhayden's code fix. I think I put it in the wrong place. Feeling stupid about now.
Thank you both dhayden and Handy Andy. You have been very encouraging. At this point you are doing a good job of keeping me from smashing things.
I just about have it! The input file looks good, and it will compile, but now I have got to get it from shutting down after I input for the first input statement. I am sure that I just have the fix between the wrong lines. Still things are looking up!
greengirl1968,

Good to hear that you are making progress. But, I think both dhayden and I need to be thinking about the fact that the input file format may not be changeable which would make inputting a string with spaces a bit more involved.

I am still thinking about the file read using the original file layout that you showed us. It is one of those problems that will bug me for awhile.

Keep working hard,

Andy
Last edited on
Greengirl1968, please post your current code AND input file.
4444
150
150
0
45.00
125.00
Circular Saw
3333
50
50
20
450.00
850.00
Cooking Range

is my current input file ordered itemId, pOrdered, pInStore, pSold, manufPrice, sellingPrice, itemName.
I am still trying to make the code from my best attempt posting work at present.
It now looks like all the items are on a line by themselves, is that correct?


Yes, it seems to make more sense that way, and it has worked for me in the past.
greengirl1986,

If you have the ability to set up the input file any way that is needed that will work.
Having the ability to put the itemName any where you would like means that you can put all the product information on one line and put itemName at the end. That makes reading the file much easier as long as each field is separated by at least a space.

Knowing this I can work on fixing the read file function.

Andy
Pages: 12