Inventory Counts

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 teo 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 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 item in the inventory, the total number of items found in each inventory, total counts and the average for eah of the two intentories. Youhave 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

count 20, 40, 109, 45, 341, 51, 513, 43, 45, 673, 982, 8, 134, 155, 632, 103, 21, 15, 49, 134

example output for inventory one

382 20
123 40

Inventory two
965 109 872 51

etc

cannot use arrays or classes. use functions, if, else, while. use <fstream><iomanip> <iostream>. THis is for basic input, please nothing fancy. I'm learning and going bald in the process. THank you to anyone who responds.

With this code, I am having an issue with my infiles. Each time I run my code, I receive my error message assigned to the infile. I'm unable to figure out why.

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

using namespace std;

int main()
{
    ofstream Inventory1;
    Inventory1.open("Inv1.out");

    ofstream Inventory2;
    Inventory2.open("Inv2.out");

    ifstream ItemInvNumber;
    ItemInvNumber.open("ItemNumber.txt");
    if(!ItemInvNumber)
       {
            cout<<" Error opening ItemNumber.txt "<<ItemInvNumber<<endl;
                return 0;
       }
    ifstream InvCnt;
    InvCnt.open("InventoryCount.txt");
    if(!InvCnt)
    {
            cout<<" Error opening InventoryCount.txt "<<InvCnt<<endl;
                return 0;
    }

        else
            {
            int item1, item2,digit,
                totalInvOne=0,
                totalInvTwo=0,
                cnt1=0,cnt2=0,
                sum=0,totcnt;

           float avg1,
                 avg2;

           string add, invno;

    while(digit!=0)
    {
        sum+=(digit%10);
      digit/=10;
    }
      return(sum);
      {
          
      
        Inventory1<<" Inventory ONE "<<endl;
        Inventory2<<" Inventory TWO "<<endl;

        if(sum<=13)
        {
            item1++;
            cnt1+=totcnt;
            Inventory1<<invno<<endl;
        }
            else
            {
                item2++;
                cnt2+=totcnt;
                Inventory2<<invno<<endl;
            }

    avg1=cnt1/item1;

    avg2=cnt2/item2;

    Inventory1<<" Total Number of Items: "<<item1<<endl<<"Total Count: "<<cnt1<<endl<< "Average: "<<avg1<<endl
    <<setprecision(3)<<fixed<<right;

    Inventory2<<" Total Number of Items: "<<item2<<endl<<"Total Count: "<<cnt2<<endl<< "Average: "<<avg2<<endl
    <<setprecision(3)<<fixed<<right;

    Inventory1.close();
    Inventory2.close();
    ItemInvNumber.close();
    InvCnt.close();
    return 0;
}
}
If you'd put down the bong and properly indent your code you'll probably see the problem. :-)

I note a strange return statement in the middle of main.

I would ditch the "else" on line 31 above. The "if" block before that exits the program so there's no need for an "else". Then you can pull the indentation back a bit for the rest of main.
420 is my birthday.....

I deleted the else on line 31, and the infile still displays the error message.

I didn't think the indention played a role on the reading of the code?

I'm very green(no pun) to this material.

I can't readily pick out the issues within a code. I don't quite understnd this material on the same level as other coders. LIke i said I'm green, very very green, almost hulk like!! lol

Thank you Dutch!
I didn't think the indention played a role on the reading of the code?

I'm not sure what you mean by that. If you mean the compiler "reading" the code, then it plays no role. But you are a human. Good indentation should help you see what your code is actually doing. And when you start showing your code to other humans, your indentation should be as perfect as possible (and standard, like spaces instead of tabs, 4 spaces per indent).

I also don't know what you mean by "the infile displays an error message". Does it display your error message? I.e, the file can't be opened? Or is it some other error message? If the file can't be opened then most likely your program just can't find it. It's probably in the wrong place. If you're on some kind of microsoft thing then people seem to have trouble finding where to put input files. Try with the exe. Try up one level from that. I don't know. Look to see where it's creating the output files.

420 is my birthday

You and you-know-who.
Last edited on
I figured out the infile issue. In my folder, I didn't have the infile.txt. I created them and added the respective information off my BlackBoard. Now I believe, i just need to finish writing the code and ensure it works. I just don't know where to start. It's easy once it's done, to see how and what everything does.
your indentation should be as perfect as possible (and standard, like spaces instead of tabs, 4 spaces per indent).


Can you show me a proper format for human reading? I am guilty I use tab a lot..
Something like this (for part of your code):

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
    Inventory1 << "Inventory ONE\n";
    Inventory2 << "Inventory TWO\n";

    if (sum <= 13)
    {
        item1++;
        cnt1 += totcnt;
        Inventory1 << invno << endl;
    }
    else
    {
        item2++;
        cnt2 += totcnt;
        Inventory2 << invno << endl;
    }

    double avg1 = double(cnt1) / item1;
    double avg2 = double(cnt2) / item2;

    Inventory1 << setprecision(3) << fixed
               << "Total Number of Items: " << item1 << endl
               << "Total Count: "           << cnt1  << endl
               << "Average: "               << avg1  << endl

    Inventory2 << setprecision(3) << fixed
               << "Total Number of Items: " << item2 << endl
               << "Total Count: "           << cnt2  << endl
               << "Average: "               << avg2  << endl 

As for your algorithm, I have no idea what you're doing to digit (or where its value is supposed to come from). And it would seem that you probably want to read from your input files at some point, presumably in a loop.
i have no idea what i am doing. I have asked chegg and other websites for help. I'm new to C++ and have ZERO idea what i am doing.
For the last 5 hours, this is what i've come up with. But it's still glitchy. I'm unsure what is wrong. The count output on my outfile is a 5 digit number and isn't the proper number that is in the infile.

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

using namespace std;

int main()
{
    ofstream Inventory1;
    Inventory1.open("Inv1.out");

    ofstream Inventory2;
    Inventory2.open("Inv2.out");

    ifstream ItemInvNumber;
    ItemInvNumber.open("Inventory1ItmNumber.txt");

    if(!ItemInvNumber)
       {
            cout<<" Error opening Inventory1ItmNumber "<<ItemInvNumber<<endl;
                return 0;
       }

    ifstream InvCnt;
    InvCnt.open("Inventory2Count.txt");

    if(!InvCnt)
    {
            cout<<" Error opening Inventory2Count.txt "<<InvCnt<<endl;
                return 0;
    }

        int itemNumber, itemCount, count=0, totalNumber1=17,
            totalNumber2=15, totalCount1, totalCount2, x, y, z, sum, total;

      float avg1,
            avg2;

       char dunce;

    Inventory1<<"Inventory ONE\n\n"
              <<"Inventory Number "
              <<setw(10)<<"Count"<<endl;

    Inventory2<<"Inventory TWO\n\n"
              <<"Inventory Number "
              <<setw(10)<<"Count"<<endl;

    while(count<31)
    {
        ItemInvNumber>>itemNumber>>dunce;

        InvCnt>>itemNumber>>dunce;

        count++;

        x=itemNumber/100%10;
        y=itemNumber/10%10;
        z=itemNumber%10;

        sum=x+y+z;

    if(sum<=13)
    {
        totalCount1=totalCount1+itemCount;

        Inventory1<<setw(10)<<itemNumber
                  <<setw(18)<<itemCount
                  <<endl;
    }
      else
      {
          totalCount2=totalCount2+itemCount;

          Inventory2<<setw(10)<<itemNumber
                    <<setw(18)<<itemCount
                    <<endl;
      }
    }




    Inventory1<<"\nTotal Number of Items: "<<totalNumber1<<endl
              <<"Total Count: "<<totalCount1<<" items"<<endl
              <<"Average: "<<avg1<<setprecision(3)<<fixed;

    avg1=totalCount1/totalNumber1;

    Inventory2<<"\nTotal Number of Items: "<<totalNumber2<<endl
              <<"Total Count: "<<totalCount2<<" items"<<endl
              <<"Average: "<<avg2<<setprecision(3)<<fixed;

    avg2=totalCount2/totalNumber2;


    Inventory1.close();
    Inventory2.close();
    ItemInvNumber.close();
    InvCnt.close();
    return 0;
}
You still need to improve your indentation. And space out your code. Don't squish things together. Proper spacing and indentation is the most important thing to learn at first.

This isn't your main problem, but it's wacky nonetheless:

 
        cout << " Error opening Inventory1ItmNumber " << ItemInvNumber << endl;

There's no meaning in tyring to print the ifstream object. I wonder what you expect it to do? Just remove it.

I can't imagine where the 17, 15, and 31 are coming from. They are very mysterious. We call them "magic numbers" and if they are actually needed (I kind of doubt it) then they should be made into constants with names that state their purpose.

Then there's stuff like this:

1
2
        ItemInvNumber >> itemNumber >> dunce;
        InvCnt >> itemNumber >> dunce;

The first reads an integer into itemNumber (then eats a comma). Then the second line reads an integer into the same variable, itemNumber. So you don't have the number you read from the first file anymore. You need to look at each line in sequence and think about stuff like that.

Speaking of sequence:

1
2
3
4
5
    Inventory1 << "\nTotal Number of Items: " << totalNumber1 << endl
               << "Total Count: " << totalCount1 << " items" << endl
               << "Average: " << avg1 << setprecision(3) << fixed;

    avg1 = totalCount1 / totalNumber1;

Here you're doing things backwards. How can you print arg1 before you've calculated it? And you need to set the precision of the output (and the fixed mode) before you print it, too. Also, you need to cast one of the integers in the division to float so that the division is done in floating point (otherwise you get "integer division" which throws away any remainder).

1
2
3
4
5
    avg1 = float(totalCount1) / totalNumber1;

    Inventory1 << "\nTotal Number of Items: " << totalNumber1 << endl
               << "Total Count: " << totalCount1 << " items" << endl
               << "Average: " <<  setprecision(3) << fixed << avg1 << endl;
Topic archived. No new replies allowed.