I am a beginner student in desperate need of help writing a program. Recently, we have learned about functions, loops and reading from data files. My assignment is to write a program (Book Sale Calculator) that accepts all input from an external data file and displays a summary for each book sale. The program needs to open the external data file and continue to read in data until the end of the file is reached. No sentinel value can be used to signal termination, and I cannot count the number of items in the file or use a count controlled loop to obtain the data. The data file is stored so that the number of books in each sale and the single character code for shipping (S = Standard which is 4.99 and E for Expedited is 12.99) representing the shipping method is on one line, and the prices for all of the books are on the second line. I need to create four functions for the following:
(1) Obtain the name of the data file and attempts to open it for readying
(2) Obtain all input for each sale from the data file. This function should also return the merchandise subtotal and shipping method
(3) Calculate all taxes and discounts
(4) Display a final total
The sales tax is .05%
Discounts are as follows:
* If the subtotal is < $50, there is no discount
* If the subtotal is between $50-$100, the discount is 10%
& If the subtotal is above $100, the discount is 15%
Additionally, no global variables can be used and all information must be shared between functions via parameters and return values. My main function should consist of variable declarations, function calls, and a control loop for reading each sale from the file can be there.
The external data file consists of the following format:
5 S
2.99 12.45 13.23 21.99 24.59
1 E
34.95
3 E
8.99 12.45 7.58
7 S
5.66 12.35 23.56 40 12.99 16.32 11.23
A sample output of the program:
The summary for order #1 is as follows:
Subtotal: 75.25
Tax: 3.76
Discount: 7.53
Shipping: 4.99
Total: 76.48
The summary for order #2 is as follows:
Subtotal: 34.95
Tax: 1.75
Discount: 0.00
Shipping: 12.99
Total:49.69
The summary for order #3 is as follows:
Subtotal: 29.02
Tax: 1.45
Discount: 0.00
Shipping: 12.99
Total: 43.46
The summary for order #4 is as follows:
Subtotal: 122.11
Tax: 6.11
Discount: 18.32
Shipping: 4.99
Total: 114.89
Thanks for shopping with us. Come again!
I have provided the source code that I have come up with so far but am at a complete loss. I really don't have any idea as to how to write this program and would GREATLY appreciate ANY assistance!!
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
|
//This program will read input from an external file and calculate the total amount of money for book sales at an online store
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
//Obtain the name of the data file and attempt to open it for reading
void openFile()
{
cout << "Book Sale Calculator" << endl;
cout << "==============================" << endl;
ifstream fp;
fp.open ("books.dat");
if (!fp)
{
cout << "Error opening the file" << endl;
exit (EXIT_FAILURE);
}
}
//Obtain all input for each sale from the data file and return merchandise total and shipping method for the sale being processed
void obtainInput()
{
while (books != 0)
{
int books;
char shipping_method;
float price; subtotal=0; shipping_price;
for (int i=0; i<=books; i++)
fp >> books;
fp >> shipping_method;
for (int i=0; i <= books; i++)
{
fp >> price;
}
subtotal+ = price[i];
}
if (shipping_method == 'S')
{
shipping_price = 4.99;
}
else
{
shipping_price = 12.99;
}
}
//Calculate tax, discount, shipping and total
void calculateOutput()
{
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()
{
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 ()
openFile();
obtainInput();
calculateOutput();
displaySummary();
return 0;
}
| |