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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
//This function displays a brief overview explaining the program to the user
void programOverview()
{
cout << "This program will read input from an external file" << endl;
cout << "and calculate the total amount of money for books sales" << endl;
cout << "at an online store." << endl;
}
//This function attempts to open the file/report error
void openFile ()
{
ifstream fp;
fp.open ("books.dat");
if (!fp)
cout << "Error opening the file" << endl;
exit (EXIT_FAILURE);
}
//This function will obtain all input for each sale from the data file and return merchandise total and shipping method for the sale being processed
void obtainInput()
{
ifstream fp;
int i=0;
int books;
char shipping_method;
float price;
float subtotal;
float shipping_price;
if (fp >> books >> shipping_method)
{
for (int i=0; i < books; i++)
{
if (fp >> price)
{
subtotal += price;
}
}
}
if (shipping_method == 'S')
shipping_price = 4.99;
else
shipping_price = 12.99;
}
//Calculate tax, discount, shipping and total
void calculateOutput(float subtotal, float shipping_price)
{
float tax;
tax = subtotal * .05;
float discount;
if (subtotal < 50)
discount = 0.00;
else if (subtotal >= 50 && subtotal <= 100)
discount = subtotal * .10;
else
discount = subtotal * .15;
float total;
total = subtotal + tax - discount + shipping_price;
}
//Display a final summary
void displaySummary(float tax, float subtotal, float discount, float shipping_price, float total)
{
cout << "The summary for the order is as follows:" << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Subtotal:" << subtotal << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Tax:" << tax << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Discount:" << discount << endl;
cout << fixed << showpoint << setprecision (2);
cout << "Shipping:" << shipping_price << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Total:" << total << endl;
}
int main ()
{
programOverview ();
cout << "Book Sale Calculator" << endl;
cout << "=======================" << endl;
openFile();
obtainInput();
calculateOutput(float subtotal, float shipping_price);
displaySummary(float tax, float subtotal, float discount, float shipping_price, float total);
return 0;
}
| |