Inventory Count (w/ functions and i/o files)

Here are my guidelines.

You are to write a program that will sum up inventory counts and report the total. The inventory is represented by a three digit inventory number and a count of the items. Sample/Test data is given below to test your program, but the real data may be more or less items than shown.
There exists a problem with the data. Two different inventories have been mixed up in the list. The inventory number and the count still match up though. You need to separate out the two inventories and get a count for each one (ie inventory ONE and inventory TWO). The R&D department has discovered that the two inventories can be distinguished by digits in the inventory number. If the digits in the inventory number sum up to be less than or equal to 13, then those items belong to inventory ONE. The others belong to inventory TWO.
Separate and compute sums for each of the two inventories and generate a total inventory count for each one. Print out each inventory, the total number of items found in each inventory, total counts and the average for each of the two inventories. You have to write to two output files. You are given sample data below.
The input files for testing your program are in two different files. The first file has the item number. The second file has the counts.
Item File: inventoryItemNbr.txt
Count File: inventoryCount.txt

Below is sample of each file:

item 382,123,965,432,823,872,734,882,117,632,664,841,149,681,616,915,323,448,552,662,811
cnt 20,40,109,45,341,51,513,43,45,673,982,8,134,155,522,632,103,21,15,49,134

Example input for one inventory
332 20
123 40
235 109
etc


I'm not very familiar with functions or reading and writing to files. but i'm getting an error on line 9, not sure what is wrong. any tips on how functions work (passing and returning values mainly) would be greatly appreciated.

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
#include <iostream>
#include <fstream>

using namespace std;
int inv1(), inv2(), cnt(), decision(), read(), open();
int main()
{
    open();
    infiletwo >> inv;
    infileone >> counts;
    decision(int inv);
    read(AC);
    return 0;
}
int open()
{
    ifstream infileone, infiletwo;
    ofstream outfileone, outfiletwo;
    infile.open("inventoryCount.txt");
    infiletwo.open("inventoryItemNbr.txt");
    outfileone.open("outone.txt");
    outfiletwo.open("outtwo.txt");
    return 0;
}
int decision(int IN)
{
    int AC;
    while(IN != 0)
    {
        AC = IN % 10;
        IN = IN / 10;
    }
        return AC;
}
int read(int AC, int IN)
{
    if (AC <= 13)
        inv1();
    else
        inv2();
}
int inv1(int IN)
{

}
int inv2()
{

}
int cnt()
{

}
Last edited on
your "infile" and other variables are defined only inside "open", but you are trying to use them outside. Shortest way to fix is to move definition to global scope. Read something on variable scopes for more information.
okay I got that fixed but how do I pass what I read from the file to "decision", and return it back when i'm done?
i'm outputting my code to a file and the console screen to see my output but my output looks like this "2686916 1963822293" in an infinite loop.
It seems like it reads the whole file as one, how do I read one piece of data from the file, use it, then go get another until there is no more data?


"inventoryItemNbr" =(299,111,382,123,777,965,432,823,872,734,882,117,632,664,841,149,681,616,915,323,448,552,662,811,921,555,760,404,534,722,399)

"inventoryCount" = (40,14,20,40,39,109,45,341,51,513,43,45,673,982,8,134,155,522,632,103,21,15,49,134,28,35,23,38,43,100,152)

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
#include <iostream>
#include <fstream>
//Jacob Wilt
//Inventory
using namespace std;
int inv1(int, int), inv2(int, int), decision(int), read(int, int, int);
    ifstream infileone, infiletwo;
    ofstream outfileone, outfiletwo;
int main()
{   int counts, inv;
    char op;
    infileone.open("inventoryCount.txt");
    infiletwo.open("inventoryItemNbr.txt");
    outfileone.open("outone.txt");
    outfiletwo.open("outtwo.txt");
    infileone >> counts >> op;
    infiletwo >> inv >> op;
    while (op != '\n')
    {
    decision(inv);
	int inven = inv;
	int AC = 0;
	int counter = counts;
    read(AC, inven, counter);
    infileone.open("inventoryCount.txt");
    infiletwo.open("inventoryItemNbr.txt");
    outfileone.open("outone.txt");
    outfiletwo.open("outtwo.txt");
    infileone >> counts >> op;
    infiletwo >> inv >> op;
    }
    infileone.close();
    infiletwo.close();
    outfileone.close();
    outfiletwo.close();
    return 0;
}
int decision(int in)
{
    int AC, A;
    while(in != 0)
    {
        A = in % 10;
        in = in / 10;
        AC = AC + AC;
    }
        return AC;
}
int read(int AC, int inven, int counter)
{
    if (AC <= 13)
    {
        inv1(inven, counter);
    }
    else
    {
        inv2(inven, counter);
    }
	return 0;
}
int inv1(int IN, int CO)
{
    outfileone << IN;
    outfileone << CO;
    cout << IN << " " << CO << endl;
	return 0;
}
int inv2(int IN, int CO)
{
    outfiletwo << IN;
    outfiletwo << CO;
    cout << IN << " " << CO << endl;
	return 0;
}
Topic archived. No new replies allowed.