Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to read details of invoices from a file and to output invoic

ID: 3847823 • Letter: W

Question

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<number of items) to read line items:

          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

Explanation / Answer

//NOTE

//Before running this program you have to create a text file named input

// I repeat again, make sure that your text file name is not input.txt , it is input only

//i.e file name is input and type is text file

//also input file must be placed in the same folder where your code is (or whatever requirement of your //compiler)

//output will be saved in a separate text file named invoice

//open inoice text file to see output of this program

//Program code

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

//we have declared this output file stream here so that all it have global scope and all function can access it
ofstream fout;


double calCost(int quantity, double cost) {
   double totalCost;
   totalCost = quantity*cost;
   return totalCost;
}

double acctotal(double totalCost, double itemCost) {
   double newTotalCost;
   newTotalCost = totalCost + itemCost;
   return newTotalCost;
}

void writeLine(string itemno, int quantity, double unitCost, double totalCost) {
   fout << left << setw(15) << itemno<< left << setw(15) << quantity << left << setw(15)<<unitCost << left << setw(15)<<totalCost <<endl;
}

void printHeader(string date) {
   fout << "Invoice date:" << date<<endl;
}

void printTotal(double invoiceTotal) {
   fout << "Total................................................" << invoiceTotal << endl;
}

int main() {
   fout.open("Invoice.txt");
   ifstream fin;
   fin.open("input.txt");
   if (fin.is_open()) {
       while (!fin.eof())
       {
           int items;
           string date;
           double invoiceCost = 0;
           while (fin>>items>>date)
           {
fout<<endl;
               printHeader(date);
               fout << left << setw(15) << "Item" <<left << setw(15)<< "Quantity" << left << setw(15) <<"Unit Price" <<left << setw(15) <<"Total Price" << endl;
               for (size_t i = 0; i < items; ++i)
               {
                   string itemno;
                   int quantity;
                   double unitCost;
                   fin >> itemno >> quantity >> unitCost;
                   double totalCost;
                   totalCost = calCost(quantity, unitCost);
                   invoiceCost = acctotal(invoiceCost, totalCost);
                   writeLine(itemno, quantity, unitCost, totalCost);
               }
printTotal(invoiceCost);
           }
       }
   }
   else {
       cout << "Error in opening input file ";
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote