///////////////////////////////////////////////////////////////////////////
// invoice.cpp
//
// Illustrates using an EOF-loop with calculations, running totals
// and averaging.
//
#include <fstream.h> // file stream definitions
#include <iomanip.h> // I/O manipulator definitions
int main( ) {
///////////////////////////////////////////////////////////////////////////
//
// Variable (and constant) declarations:
int SKU; // inventory number of item
int UnitsThisItem; // number of units of current item bought
float CostPerUnit; // cost per unit of current item
float CostThisItem; // cost of all units of current item
int TotalUnits = 0; // total number of units bought so far
float TotalCost = 0.0f; // total cost of items bought so far;
// The "f" specifies the const. value is
// a float (instead of a double).
float AvgUnitCost; // average cost per unit of items bought
float SalesTax; // sales tax charged on total
const float SalesTaxRate = 0.045f; // sales tax rate
///////////////////////////////////////////////////////////////////////////
// Declare file stream variables, open files, other I/O setup:
//
ifstream myInput; // input file stream
ofstream myOutput; // output file stream
myInput.open("order.dat"); // open files
myOutput.open("invoice.out");
// enable manipulators
myOutput.setf(ios::fixed, ios::floatfield);
myOutput.setf(ios::showpoint);
///////////////////////////////////////////////////////////////////////////
// Write header to output file; you only do this once, so it goes before
// the while loop.
//
myOutput << "Customer Invoice:" << endl << endl;
myOutput << "SKU Units Unit Price Extended" << endl;
myOutput << "--------------------------------------" << endl;
///////////////////////////////////////////////////////////////////////////
// Attempt to read first line of data from input file (priming read):
//
myInput >> SKU >> UnitsThisItem >> CostPerUnit;
while (myInput) {
/////////////////////////////////////////////////////////////////////
// Update running totals and calculate total cost of current item:
//
TotalUnits = TotalUnits + UnitsThisItem; // running total of units
CostThisItem = UnitsThisItem * CostPerUnit; // total cost this item
TotalCost = TotalCost + CostThisItem; // running total of cost
/////////////////////////////////////////////////////////////////////
// Write inventory number, # of units, unit cost and total cost of
// current item to output file:
//
myOutput << setw(4) << SKU;
myOutput << setw(8) << UnitsThisItem;
myOutput << setw(13) << setprecision(2) << CostPerUnit;
myOutput << setw(13) << setprecision(2) << CostThisItem;
myOutput << endl;
/////////////////////////////////////////////////////////////////////
// Attempt to read next line of data from input file:
//
myInput >> SKU >> UnitsThisItem >> CostPerUnit;
} // end of while (myInput)
////////////////////////////////////////////////////////////////////////
// Calculate average cost per unit of order, guarding against division
// by zero:
//
if (TotalUnits > 0) {
AvgUnitCost = TotalCost / TotalUnits;
}
////////////////////////////////////////////////////////////////////////
// Calculate sales tax on order (actually a subtle inadequacy here):
//
SalesTax = SalesTaxRate * TotalCost;
////////////////////////////////////////////////////////////////////////
// Write order totals to output file:
//
myOutput << "--------------------------------------" << endl;
myOutput << " Total Price:";
myOutput << setw(13) << setprecision(2) << TotalCost;
myOutput << endl;
myOutput << " Sales Tax :";
myOutput << setw(13) << setprecision(2) << SalesTax;
myOutput << endl;
myOutput << " Grand Total:";
myOutput << setw(13) << setprecision(2) << (TotalCost + SalesTax);
myOutput << endl;
myOutput << " -------------------------";
myOutput << endl << endl;
////////////////////////////////////////////////////////////////////////
// Write average cost per unit to output file:
//
myOutput << "Average cost per unit:";
myOutput << setw(8) << setprecision(2) << AvgUnitCost;
myOutput << endl;
////////////////////////////////////////////////////////////////////////
// Close input and output files:
//
myInput.close();
myOutput.close();
////////////////////////////////////////////////////////////////////////
// Return total number of units (just for the heck of it):
//
return TotalUnits;
} // end of main()