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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
usingnamespace 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;
}
}
> 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.
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.