Summing an Array with data from file resulting in enormous numbers
May 4, 2015 at 6:47pm UTC
When my output gets processed I'm getting giant numbers where I'm trying to sum the array's and I cannot figure out why. The data from the text file outputs correctly, but when I try to use the data in the oostArray[999] something messes up. What am I missing?
the cost.txt looks like
Books 45.01
Pens 21.03
Pencils 10.90
Hats 50.00
Caps 800.00
Food 1.00
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
//CALCULATE SALES ITEMS FROM FILE
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
float totalPrice;
float totalSalesTax;
float grandTotal;
string itemArray[999];
float costArray[999];
float taxArray[999];
char runAgain;
do
{//RunAgain loop
//open file
ifstream salesFile("cost.txt" );
if (!salesFile)//Check if opened
{
cout << "Couldn't open file!" <<endl;
return 1;
}
int count = 0;
//store data from files in array
while (salesFile >>itemArray[count] >>costArray[count])
{
count++;
}
//close file
salesFile.close();
//Calculate Sales Tax 6%
for ( int c = 0; c < count; c++)
{
taxArray[c]+=(costArray[c] *.06);
}
//Calculations
for (int x=0; x < count; x++) //Total Sales Price
{
totalPrice+=costArray[x];
}
for (int v=0; v < count; v++)//Total Sales Tax
totalSalesTax+=taxArray[v];
//Grand Total
grandTotal = totalPrice + totalSalesTax;
//Display output
cout << "****************************************************" << endl;
cout << "****************************************************" << endl;
cout << "******** S A L E S R E C E I P T ********" << endl;
cout << "****************************************************" << endl;
cout << "** ** ** ** **" << endl;
cout << "** Item Names ** ** Price ** Tax **" << endl;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "****************************************************" << endl;
for (int cnt = 0; cnt <count; cnt++)
{
cout << "** " << itemArray[cnt] <<setw(20-itemArray[cnt].length()) << " $" << setw(8) << costArray[cnt] << setw(15) << taxArray[cnt] <<" **" <<endl;
}
cout << "****************************************************" << endl;
cout << "** Total Price $" << setw(10) << totalPrice << " **" << endl;
cout << "** Total Sales Tax $" << setw(10) << totalSalesTax << " **" << endl;
cout << "** ----------- **" << endl;
cout << "** Grand Total $" << setw(10) << grandTotal << " **" << endl;
cout << "** **" << endl;
cout << "****************************************************" << endl;
cout << "****************************************************" << endl;
//Run again input
cout << "Do you want to calculate another file worth of sales items? (Y/N):" << endl;
cin >> runAgain;
runAgain = tolower(runAgain);
system("CLS" );
}// End loop
while (runAgain == 'y' );
return 0;
}
Last edited on May 4, 2015 at 6:48pm UTC
May 4, 2015 at 6:50pm UTC
totalPrice=0
maybe
May 4, 2015 at 7:01pm UTC
+1 LendraDwi
Also consider initializing totalSalesTax
.
May 4, 2015 at 7:07pm UTC
I believe the error is here :
1 2 3 4 5
//Calculate Sales Tax 6%
for ( int c = 0; c < count; c++)
{
taxArray[c]+=(costArray[c] *.06);
}
Try changing it to :
1 2 3 4 5
//Calculate Sales Tax 6%
for (int c = 0; c < count; c++)
{
taxArray[c] = (costArray[c] * .06);
}
May 4, 2015 at 7:09pm UTC
Okay I initialized both totalPrice = 0;
and totalSalesTax =0;
and that fixed the Total Price output, but the summing of taxArray is still outputting these huge numbers.
I've summed plenty of arrays before and my code is no different from other programs I've done. Do you think this is causing my problem? Can you not do that?
1 2 3 4 5
//Calculate Sales Tax 6%
for ( int c = 0; c < count; c++)
{
taxArray[c]+=(costArray[c] *.06);
}
May 4, 2015 at 7:14pm UTC
taxArray[c]+=(costArray[c] *.06);
is equivalent to :
taxArray[c] = taxArray[c] + (costArray[c] *.06);
But taxArray[c] has no value since the array in not initialised so it contains "garbage" which is why you get the huge numbers.
May 4, 2015 at 7:15pm UTC
Hey I was typing my reply when you set yours konstance. You are right it was the += messing it up. Thanks for the help everyone!
Topic archived. No new replies allowed.