You are required to design a program invoice.cpp that will be used to calculate the total cost
for a list of purchased items. Each purchased item will be presented by its productId, the price
per item, and the corresponding quantity. We assume that such purchased items are recorded
in a plain text file purchase.dat in the form similar to the following sample
P010001 24.99 1
QX-35 19.99 2
DVD-player5 58.95 1
P010002 8.85 20
...
that is, the first column represents the product ID which will be alphanumeric without white
spaces, the second column represents the product price in Australian dollars, and the third
column represents the quantity of the corresponding purchased items. Your program
invoice.exe compiled from invoice.cpp will then be able to generate the invoice via
invoice.exe < purchase.dat > statement.txt
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
#include <iomanip> // needed for formatting
using namespace std;
int main()
{
string filename = "prices.dat"; // put the filename up front
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
// set the output file stream formats
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
// send data to the file
outFile << setw(3) << "PHANTOM COMPAY INVOICE" << "\n" << endl
<< "Product ID Quantity Price ($) Cost ($)" << endl
<< "---------- -------- --------- --------" << endl
<< setw(4) << "P010001" << 1 << 24.99 << 24.99 << endl
<< setw(3) << "QX-35" << 2 << 19.99 << 49.98 << endl
<< setw(3) << "DVD-player5" << 1 << 58.95 << 58.95 << endl
<< setw(3) << "P010002 " << 20 << 8.85 << 177.00 << endl;
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
system("pause");
return 0;
}

New Topic/Question
Reply




MultiQuote




|