Hi so I have this assignment for one of my classes, im having some issues with it. I was hoping someone could have a look at it and tell me what i am doing wrong and what I need to fix. Thanks!

heres what the assignment is:

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.

and heres my code so far:

#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;
	int department;
	int item;
	int purchase;
	int discount;
	int net;
	int net_total;
	int total;
	int service_charge;
	int total_purchase, total_discount, subtotal, SalesTax; 
	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;
			}
		}

		if ( department == 1 || department == 2)
		{
			if ( purchase >= 10 )
			{
				discount = purchase * 0.05;
				net = purchase - discount;
			}
		}

		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

Recommended Answers

All 21 Replies

What exactly are you having issues with? Are you getting unexpected output, or does it not even compile?

Just things I noticed while reading, though:

Lines 70-95, those if statements are using the assignment operator ("=") instead of the logical equal to ("=="). Also, it looks like you are doing the same calculation for each department, so why even both with ifs?

Line 101, you should have "subtotal >= 50" otherwise if the subtotal is exactly 50 it would fail both if statements.

it fails to compile, here are the errors:

Run-Time Check Failure #3 - The variable 'total_purchase' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'total_discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net_total' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'net' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'discount' is being used without being initialized.
The thread 'Main Thread' (0x608) has exited with code -1073741510 (0xc000013a).
The program '[1020] asgn3.exe: Native' has exited with code -1073741510 (0xc000013a).

You should initialize your variables before using them on the left side of an assignment.

eg.

total_purchase += purchase;

is in fact

total_purchase = total_purchase + purchase;

and total_purchase does not have a value in the addition.

Should be the right side of course.

What do you mean? sorry im really bad at this

You declare your variables as

int total_purchase, total_discount, subtotal, SalesTax;

But they are not initialized. Try

int total_purchase(0), total_discount(0), subtotal(0), SalesTax(0);

I tried and made the changes but got the same result

???

Did you initialize all you variables ?? Or just the examples I mentioned ??

What I meant was that you always must initialize your variables variables....

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;

okay that fixed the problem, sorry im slow...its working now but its giving the wrong result, this is the reult.txt im getting

HIGH FASHION DEPARTMENT STORE
MONTHLY BILLING STATEMENT
132


DEPT NBR ITEM NBR AMOUNT OF PURCHASE DISCOUNT AMOUNT NET AMOUNT
---------------------------------------------------------------

4 12 8 0 0
12 8 0 0
12 8 0 0
12 8 0 0
NET TOTAL AMOUNT 0**
SALES TAX AT 7% 0
SERVICE CHARGE 0

FINAL TOTAL - (PLEASE PAY THIS AMOUNT) 0***

Line 54: you have no else for this if statement, so if "purchase" is less than 25 you never set "net" to anything.

Same thing for the if at line 63

Well, I should not do your homework, but try to find a way to read the input file, record by record. In your current program you wont read more than 1 row from the input file.

i added in else statments but im not sure how to run them right

#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

else statements are set up just like if statements, but without the conditional following.

EXAMPLE:

if (condition == true)
{
    do something;
    do another thing;
}
else
{
    do a different thing;
    do more different things;
}

But in short, Lines 60 and 70 still need a semicolon at the end.

Okay i kinds tried to fix my if statements, but now its saying my "total_purchase" variable is being used without being initialized, but i thought i already fixed that?

here my updated code:

#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

I compiled it and did not get that warning, as it seems you have initialized it properly. There are a host of other errors. Does line 77 look right to you? There are 6 more errors like that. Someone had pointed that out in an earlier post.

Also, for example, on line 68, you're mixing doubles and ints, which is ok, but you're assigning it back to an int, which will truncate the decimals off of your value.

A minor thing, you have your constants defined at the top, but you hard coded the values in later (for 0.08, etc.).

EDIT: You are missing a couple of semicolons too.

are you saying i need else statements for like 77 and so on?

Nope. if (anything = 4) is always true (the only false value would be for 0). Reread your section on comparison operators to assure yourself that == is what is needed there.

okay ive made changes and stuff but now its saying that my variables rnt initiallized (which i know they are?) and its giving me a weird output

Can someone run my code and tell me if it works for them?

heres my data text:

132 2 12 8.97
132 2 65 19.80
132 3 198 67.90
132 3 292 7.77
132 3 418 18.90
132 4 190 25.91
254 1 55 25.50
254 1 678 69.80
254 3 66 99.0
254 4 57 80.70
254 4 78 990.40
254 4 97 70.50
315 1 25 15.5
315 1 35 9.9
315 1 245 89.9
315 2 37 53.2
315 2 57 100.25
315 3 28 99.9
315 3 245 1000.0
315 3 355 3000.15
315 4 578 256.7
315 4 653 356.25


please and thanks!

Please post the current code with which you are working.

#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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.