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

Click Here

First off, as I have said elsewhere, in C++ main() should always have a return type of int. Some older compilers allowed void main(), but it is actually not permissible under the standard. It should return zero for a successful program run or an error code on failure.

Second, the conio library is non-standard, and since the only place you use it is to pause the screen when the you reach the end of the program - something that is only necessary under some older IDEs such as Bloodshed Dev-C++ - you can safely eliminate it if you upgrade to an IDE that isn't over ten years old, such as the Orwell fork of Dev-C++ or Code::Blocks. Both are in current development, and both have fixed the particular bug that work-around was needed for.

Third, the instructions say to break the problem down into at least two functions, presumably in addition to main(). I would go further than that myself, but that's the specific requirement of the assignment, and even if you are doing this for your own personal study, it is a good practice in any case.

Getting (finally) to your code, well, to start with, you are IMAO hard-coding too many aspects of the program, and not letting the data drive the flow of control. There are several things I can recommend, starting with making a struct type or even a class to hold the department information. Something like this is what I have in mind:

struct Item
{
    std::string name;
    double price;
    unsigned int quantity;
};

struct Department
{
    double disc_minimum;
    double discount;
    std::vector<Item> purchases;
};

vector<Department> dept;

void init_dept(vector<Department> dept) 
{
    dept[0].discount_minimum = 10.00;
    dept[0].discount = 0.05;
    dept[1].discount_minimum = 10.00;
    dept[1].discount = 0.05;
    dept[2].discount_minimum = 25.00;
    dept[2].discount = 0.08;
    dept[3].discount_minimum = 25.00;
    dept[3].discount = 0.08;
}

This may seem like overkill, but I'd bet that it would eliminate at least a quarter of the code you have now, just by making it a simple matter of simplifying the checks on the discounts. While this is a bit much for something this size, it becomes a very useful approach as the size of the dataset increases, and it makes changes easier as well.

I still haven't addressed your question, I know. The real answer is, there doesn't need to be one, and probably should be one. You would be better off writing a function that prints the output, and loops on the dept[] array:

void underline(unsigned int length)
{
    outs << setw(length) << setfill('-') << endl;
}


void print_receipt(ostream& outs, Department dept[], unsigned short total_depts)
{
    // bill display
    outs << " HIGH FASHION DEPARTMENT STORE" << endl;
    outs << " MONTHLY BILLING STATEMENT FOR ";
    outs << customer << endl;
    outs << endl << endl;
    outs << std::setw(20) << setfill(' ')
         << std::setw(20) << "ITEM NAME " 
         << std::setw(8)  << setprecision(2) << " PRICE " 
         << std::setw(8)  << " QUANTITY "
         << std::setw(8)  << setprecision(2) << " DISCOUNT AMOUNT " 
         << std::setw(8)  << setprecision(2) << " NET AMOUNT " 
         << endl;
    underline(40);  

    for (int i = 0; i < total_depts; i++)
    {
        cout << "Department " << i << ':' << endl;
        underline(12);

        for (int j = 0; j < dept[i].purchases.size(); j++)
        {
            double price = dept[i].purchase[j].price;
            double quantity = dept[i].purchase[j].quantity;
            double subtotal = price * quantity;
            double discount = (subtotal > dept[department].disc_minimum) ? 
                              dept[department].discount : 0.0;
            double discount_amt = subtotal * discount;
            double net = subtotal - discount_amt;

            outs << std::setw(20) << setfill(' ')
                 << std::setw(20) << dept[i].purchase[j].name 
                 << std::setw(8)  << setprecision(2) << price
                 << std::setw(8)  << quantity
                 << std::setw(8)  << setprecision(2) 
                 << (discount_amt > 0) ? discount_amt : ' '
                 << std::setw(8)  << setprecision(2) << net                 
                 << endl;
        } 
    }

    // I'll leave the rest for you
}

Edited 1 Year Ago by Schol-R-LEA

This article has been dead for over six months. Start a new discussion instead.