Hello , I've been trying to figure out this code and determine what's causing the miscalculation on it.
Everything seems right for calculation , I don't get why there is if else statement for output screen.
This is just a practice question for myself.
The High Fashion Department Store wants a program written to produce its monthly bills. Input to
the program will be the customer number, the number of the department in which that item was
purchased, the item number, and the amount of the purchase. There are four departments numbered
1 through 4. On all purchases of $10.00 or more in departments 1 and 2, the customer receives a 5
percent discount. On all purchases of $25.00 or more in departments 3 and 4, the customer receives
an 8 percent discount. Totals are to be produced for each department and for each customer. Sales
tax of 7 percent is paid on the net total for the customer. Also a service charge is added to the bill if
the amount of the bill is less than $100.00. The service charge is 10 percent of the net bill before the
sales tax is added if the total is less than $50.00. Otherwise, the service charge is 5 percent. The bill
for each customer is to be printed on a separate page. (You need a counter to keep track of the
number of print lines used.)
You have to use at least two functions for this assignment.
Make sure error checks are performed on the input data.
Input should be read from a data file and output to a data file.
#include<fstream>
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
#define in_file "data.txt"
#define out_file "result.txt"
void main()
{
ifstream ins;
ofstream outs;
//open file
ins.open(in_file);
outs.open(out_file);
//declare variables and constants
const double discount1 = 0.05;
const double discount2 = 0.08;
const double sales_tax = 0.07;
const double service_charge1 = 0.10;
const double service_charge2 = 0.05;
int customer(0);
int department(0);
int item(0);
int purchase(0);
int discount(0);
int net(0);
int net_total(0);
int total(0);
int service_charge(0);
int total_purchase(0), total_discount(0), subtotal(0), SalesTax(0);
int counter = 0;
if (!in_file)
{
cout << "Please input a proper input file." << endl;
}
else
{
ins >> customer;
if (counter < 4)
{
ins >> department >> item >> purchase >> customer;
counter++;
//calculations
if (department == 3 || department == 4)
{
if (purchase >= 25)
{
discount = purchase * 0.08;
net = purchase - discount;
}
}
else
{
net = purchase;
}
if (department == 1 || department == 2)
{
if (purchase >= 10)
{
discount = purchase * 0.05;
net = purchase - discount;
}
else
{
net = purchase;
}
}
if (department == 1)
{
total_purchase += purchase;
total_discount += discount;
net_total += net;
}
if (department == 2)
{
total_purchase += purchase;
total_discount += discount;
net_total += net;
}
if (department == 3)
{
total_purchase += purchase;
total_discount += discount;
net_total += net;
}
if (department == 4)
{
total_purchase += purchase;
total_discount += discount;
net_total += net;
}
net_total += net_total;
SalesTax = (net_total * sales_tax);
subtotal = net_total + SalesTax;
if (subtotal < 100 && subtotal > 50)
{
service_charge = net_total * service_charge1;
total = subtotal + service_charge;
}
if (subtotal < 50)
{
service_charge = net_total * service_charge2;
total = subtotal + service_charge;
}
//bill display
outs << " HIGH FASHION DEPARTMENT STORE" << endl;
outs << " MONTHLY BILLING STATEMENT" << endl;
ins >> customer;
outs << " " << customer << endl;
outs << endl << endl;
outs << "DEPT NBR " << " ITEM NBR " << " AMOUNT OF PURCHASE " << " DISCOUNT AMOUNT " << " NET AMOUNT " << endl;
outs << "---------------------------------------------------------------" << endl;
outs << endl;
outs << department << " " << item << " " << purchase << " " << discount << " " << net << endl;
if (department == 2)
{
outs << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
else
{
outs << "TOTAL DEPT 2 " << total_purchase << " " << total_discount << " " << net_total << "*" << endl;
outs << endl;
outs << department << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
if (department == 3)
{
outs << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
else
{
outs << "TOTAL DEPT 3 " << total_purchase << " " << total_discount << " " << net_total << "*" << endl;
outs << endl;
outs << department << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
if (department == 4)
{
outs << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
else
{
outs << "TOTAL DEPT 4 " << total_purchase << " " << total_discount << " " << net_total << "*" << endl;
outs << endl;
outs << department << " " << item << " " << purchase << " " << discount << " " << net << endl;
}
outs << " NET TOTAL AMOUNT " << net_total << "**" << endl;
outs << " SALES TAX AT 7% " << SalesTax << endl;
outs << " SERVICE CHARGE " << service_charge << endl;
outs << endl;
outs << "FINAL TOTAL - (PLEASE PAY THIS AMOUNT) " << total << "***" << endl;
}
}
_getch();
ins.close();
outs.close();
}//end program
this is the output