[try Beta version]
Not logged in

 
Help on an inventory program, almost have it.

Jun 30, 2019 at 2:55pm
Hey guys! I have a problem where we have to read in two data files. One is the inventory number and one is the count of how many items each number has. The inventory number when broken apart can be added and if it is less than or equal to 13 it goes in one file. If it is greater than it goes in another. I have everything running perfectly, but we have to count the total number of items in each inventory. I am so close. The problem is this is a beginner's class and we can't use advanced techniques we haven't been taught yet. I'll try to list what we haven't been taught:
for loops
arrays
functions
do/while

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

using namespace std;

int main() 
{
    
    
    int itemNumber;
    int itemCount;     //i cheated and added 17 and 15, but I need to figure 
    int count=0;       //out how to get it to count those. 
    int totalNumber1=17; //the total number of items in the first inventory
    int totalNumber2=15; //the total number of items in the second inventory
    int totalCount1;  //the total number of counts in the first inventory 
    int totalCount2;  //the total number of counts in the second inventory
    int a, b, c, sum; 
    float average1, average2;
    int total;
    char dummy;  //bogus character to read the comma for the input file
 
    if(sum<=13)
    {
        totalCount1=totalCount1+itemCount;
        outInventory1<<setw(9)<<itemNumber<<setw(16)<<itemCount<<endl;
    }
      
    else
    {
        totalCount2=totalCount2+itemCount;
        outInventory2<<setw(9)<<itemNumber<<setw(16)<<itemCount<<endl;
    }
    }
    
    
     

Last edited on Jul 1, 2019 at 8:35pm
Jun 30, 2019 at 2:58pm
> int totalCount1; //the total number of counts in the first inventory
> int totalCount2; //the total number of counts in the second inventory
Get into the habit of initialising all your variables.

Jul 1, 2019 at 3:35pm
I didn't realize how much of a hint you were giving me. Thanks, I got it figured out.
Jul 1, 2019 at 6:35pm
I'm not understanding the hint. Did salem remove the values assigned to int totalcount1 and totalcount2?
Jul 1, 2019 at 6:44pm
CodeNovice01, what is the value of totalcount1 before it gets incremented on line 62?
Jul 1, 2019 at 7:09pm
Ganado, there isn't a value?! Sorry if i seem dense, it's because i am.
Jul 1, 2019 at 7:42pm
The issue is the value is uninitialized. It's essentially garbage because it was never given a proper value. This is undefined behavior, but what it most likely means is that the value will be garbage,
and incrementing garbage ( {garbage} = {garbage} + 1; ) is still some garbage value.

This is why salem c said to get in the habit of initializing variables.
Last edited on Jul 1, 2019 at 7:46pm
Topic archived. No new replies allowed.