Help please with receipt calculator!

How do I get this to work properly...

I want it to output the item price and sales tax and total with sales tax for each item the user enters.
And the item total, tax total and subtotal.

I managed to get the item total, tax total and subtotal to work just not listing individual items.

I want output to look like this:
-----------------------------------------
Item Cost Item Tax Item Subtotal
-----------------------------------------
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 0.00
-----------------------------------------
Item Total Tax Total Total Due
0.00 0.00 0.00


/////////////////////////////
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{

//Variable
float item, cost, tax, subtotal;
double sales = 0.0;
int numSales = 0;
double totalSales = 0.0;

item = 0;



cout << "First sales amount (Enter a negative number to stop): ";
cin >> sales;

while (sales >= 0.0)

{
numSales = numSales + 1;

totalSales = totalSales +
sales;

cout << "Next sales amount (Enter a negative number to stop): ";
cin >> sales;
}
//end while


//Calculations

tax = totalSales * .07;
subtotal = tax + totalSales;


//Output
cout << "----------------------------------------- " << endl;
cout << " " << endl;
cout << " Receipt of Purchase" << endl;
cout << fixed << setprecision(2) << endl;
cout << "----------------------------------------- " << endl;
cout << setw(10) << "Item Cost" <<
setw(10) << "Item Tax" <<
setw(10) << " Item Subtotal" << '\n';
cout << "----------------------------------------- " << endl;
cout << setw(10) << "Item Total" <<
setw(10) << "Tax Total" <<
setw(10) << "Total Due" << '\n';
cout << setw(10) << totalSales <<
setw(10) <<tax <<
setw(10) <<subtotal << '\n';
cout << "----------------------------------------- " << endl;
cout << "You purchased " << numSales << " items." << endl;
cout << "----------------------------------------- " << endl;
cout << "Thank you! Have a nice day!" << endl;
cout << "----------------------------------------- " << endl;
//system("pause");
return 0;
}
//end of main function
@xokittenxo

In order to get the results you want, you would need to make a struct, or an array, as you are just adding everything together and have no way to separate each purchase to show on the receipt.

Here's one way of finishing your program..

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
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

struct Item_Receipt
{
	double item;
	double cost;
	double tax;
	double subtotal;
};

int main()
{
	vector <Item_Receipt> Store_Receipt;
	Item_Receipt Purchase;

	//Variables
	const double item_tax = .08; // New York tax anyway. Change to your State Tax rate
	double Item_Total = 0.0;
	double Tax_Total = 0.0;
	double Total_Sales = 0.0;
	int numSales = 0;

	cout << "First sales amount (Enter a 0 to stop): ";
	cin >> Purchase.item;
	Purchase.tax = Purchase.item * item_tax; 
	Purchase.subtotal = Purchase.item + Purchase.tax;
	Store_Receipt.push_back(Purchase);
	Item_Total += Purchase.item;
	Tax_Total += Purchase.tax;
	Total_Sales += Purchase.subtotal;
	numSales++;
	while (Purchase.item > 0.0)
	{
		cout << "Next sales amount (Enter a 0 to stop): ";

		cin >> Purchase.item;

		if(Purchase.item > 0.0)
		{
			Purchase.tax = Purchase.item * item_tax;
			Purchase.subtotal = Purchase.item + Purchase.tax;
			Store_Receipt.push_back(Purchase);

			Item_Total += Purchase.item;
			Tax_Total += Purchase.tax;
			Total_Sales += Purchase.subtotal;

			numSales++;
		}
		else
			cout << endl << "That was the last item being puchased.\nHere is your itemized receipt." << endl << endl;
	}
	//end while

	//Output
	cout << "----------------------------------------- " << endl;
	cout << "\tReceipt of Purchase" << endl;
	cout << "----------------------------------------- " << endl << endl;
	cout << fixed << setprecision(2);
	cout << setw(10) << "Item Cost" <<
		setw(15) << "Item Tax" <<
		setw(15) << "Subtotal" << '\n';
	cout << "----------------------------------------- " << endl;
	for(int x=0;x<numSales;x++)
		cout << setw(8) << Store_Receipt[x].item << setw(15) << Store_Receipt[x].tax <<
		setw(15) << Store_Receipt[x].subtotal << endl;
	cout << "----------------------------------------- " << endl;
	cout << setw(10) << "Item Total" <<
		setw(15) << "Tax Total" <<
		setw(15) << "Total Due" << endl;
	cout << setw(8) << Item_Total << setw(15) << Tax_Total <<
		setw(15) << Total_Sales << endl;
	cout << "----------------------------------------- " << endl;
	cout << "\tYou purchased " << numSales << " items." << endl;
	cout << "----------------------------------------- " << endl;
	cout << "\tThank you! Have a nice day!" << endl;
	cout << "----------------------------------------- " << endl;
	cin >> numSales; // Just to prevent screen from closing
	return 0;
}
Last edited on
Thank you so much for your help. I knew I could make it into an array but I was having problems with the code. and I didn't know I could use a struct/ how to use it. Again thank you! I really appreciate it. I understand how to do this properly with a struct.
@xokittenxo

You're welcome. If you do have any questions or just want confirmation on a code section, please ask. I'll be glad to help out.
Topic archived. No new replies allowed.