This program takes code from a file cost.txt and output some calculations and then writes the output to a file cost.out
cost.txt looks like this
Books 45.01
Pens 21.03
Pencils 10.90
Hats 50.00
Caps 800.00
Food 1.00
When I run my code I get all the correct formatting and output, but when I look at the file it's printed it looks like this
****************************************************
****************************************************
******** S A L E S R E C E I P T ********
****************************************************
** ** ** ** **
** Item Names ** ** Price ** Tax **
****************************************************
****************************************************
** Total Price $ 927.94 **
** Total Sales Tax $ 55.68 **
** ----------- **
** Grand Total $ 983.62 **
** **
****************************************************
****************************************************
You can see that the area where the item names price and tax on that item is supposed to go doesnt show up. I can only assume that its from this line
1 2 3 4 5 6
|
for (int cat = 0; cnt <count; cat++)
{
out_data << "** "<< itemArray[cnt] <<setw(20-itemArray[cnt].length()) << "$" <<setw(8)<< costArray[cnt] << " $" << setw(5) << taxArray[cnt] <<" **"<<endl;
}
|
But I don't understand why because I have another program that does printing similar to this and it also uses a for loop, but it actually prints the output to the text file.
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
//CALCULATE SALES ITEMS FROM FILE
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
float totalPrice = 0;
float totalSalesTax = 0;
float grandTotal = 0;
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;
}
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(5) << 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;
//print output to file
ofstream out_data("costout.txt");
out_data << "****************************************************" << endl;
out_data << "****************************************************" << endl;
out_data << "******** S A L E S R E C E I P T ********" << endl;
out_data << "****************************************************" << endl;
out_data << "** ** ** ** **" << endl;
out_data << "** Item Names ** ** Price ** Tax **"<< endl;
out_data<< setiosflags(ios::fixed) << setprecision(2);
out_data<< "****************************************************" << endl;
for (int cat = 0; cnt <count; cat++)
{
out_data << "** "<< itemArray[cnt] <<setw(20-itemArray[cnt].length()) << "$" <<setw(8)<< costArray[cnt] << " $" << setw(5) << taxArray[cnt] <<" **"<<endl;
}
out_data << "****************************************************" << endl;
out_data << "** Total Price $" << setw(10) << totalPrice << " **" << endl;
out_data << "** Total Sales Tax $" << setw(10) << totalSalesTax << " **"<< endl;
out_data << "** ----------- **" << endl;
out_data << "** Grand Total $" << setw(10) << grandTotal << " **" << endl;
out_data << "** **" << endl;
out_data << "****************************************************" << endl;
out_data << "****************************************************" << 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;
}//End SALES ITEMS FROM FILE
|