Question: CMPSC-121 Project #2 Write a C++ program to read details of invoices from a file and to output invoices which indicate the total cost of
CMPSC-121 Project #2
Write a C++ program to read details of invoices from a file and to output invoices which indicate the total cost of each item and the total cost of the invoice together with full details.
Details of an invoice are available as follows:
The number of items on the invoice, the date of the invoice
For each item, an item code (6 digits), a quantity and a unit cost.
Thus a typical set of lines in the file for invoices might be:
3 10/22/2014
161432 5 6.50
543289 10 2.25
876234 2 10.75
2 10/25/2014
135876 12 22.50
543287 4 19.25
The above indicates that there are three items on the first invoice and its date is 3/15/2014, the first item has an item code of 161432, an order quantity of 5 and a unit price of $6.50. There are two items on the next invoice, etc.
Write a C++ program to read the file (input.txt) with details of invoices and to output to a file an invoice which indicates the total cost of each item and the total cost of the invoice together with full details. The above details might produce a file as follows:
Invoice date: 10/22/2014
Item Quantity Unit Price Total Price
161432 5 6.50 32.50
543289 10 2.25 22.50
876234 2 10.75 21.50
Total 76.50
Invoice date: 10/25/2014
Item Quantity Unit Price Total Price
135876 12 22.50 270.00
543287 4 19.25 77.00
Total 347.00
The functions could be specified as follows:
Function type : double
Function name : calccost
Operation : Calculates the cost for a single item.
Description : Given the unit price of an item in
dollars and cents and the quantity of
the item, it calculates the total cost in
dollars and cents
Parameters : int quantity
double unitcost
Return : totalCost
--------------------------------------------------------------------------
Function type : double
Function name : acctotal
Operation : Accumulates the total cost of invoice
Description : Given current total invoice cost and
the total cost of an invoice item
calculates the new total invoice cost.
Parameters : double totalCost
double itemCost
Return : newTotalCost --------------------------------------------------------------------------
Function type : void
Function name : writeLine
Operation : Writes a line of the invoice.
Description : Given the item reference number, the
quantity, the unit price and total
price of an item, outputs a line of
the invoice.
Parameters : string itemno
int quantity
double unitCost
double totalCost --------------------------------------------------------------------------
Function type : void
Function name : printHeader
Operation : Writes Invoice date: and the date to the file
Description : Accepts the date and writes Invoice date: followed by the date into the file. Parameters : string date -------------------------------------------------------------------------- Function type : void
Function name : printTotal
Operation : Writes Total followed by the total and then an endl in the file.
Description : Accepts the total and writes it followed by a line of dots and then the invoice total and an endl date into the file. Parameters : double invoiceTotal
The pseudocode for main would be:
Open output file (Before main, create output file with the line: ofstream fout;
Create and open input file
Be sure to test to see if you can read the file. If not display a message and end the program.
Read number of items and date. Date can just be a string. This will be the main read loop which will end the program when it encounters end of file
while( fin >> items >> date)
totalCost = 0
printHeader (date)
Use a for loop (i=0;i fin >> item >> quantity >> unitPrice totalcost = calcCost(quantity, unitPrice) invoiceCost = accTotal(invoiceCost, totalCost) writeLine(itemno, quantity, unitCost, totalCost) end for printTotal(invoiceCost) End main read loop Close input and output files To submit this project for grading, upload the .cpp file only to the Project 2 dropbox. See instructor for help as needed.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
