Help Please Homework

May 7, 2021 at 7:30am
Write your question here.
hello I am BEGINNER and need some help with my code, it's not giving the right out put. it should show a sub total and total after tax. Please help!!!
Category 1 - Clothing: 6%
Category 2 - Beauty products: 7%
Category 3 - Grocery: 3%
Category 4 - Gardening: 6%
Category 5 - School supplies: 3%
Category 6 - Tobacco products: 10%
Creates an array to store the numbers users input and uses the switch statement to calculate the sales tax and final cost based on the category of the purchased product
Prompts the user for category and price of the product
Calculates and displays the final cost

[code]
#include<iostream>

using namespace std;

#define SIZE 100

int main() {

int category[SIZE];

double price[SIZE], sales_tax = 0, sub_total = 0;

int n = 0;

while (1) {

cout << "\n\nCategory 1 - Clothing" << endl;

cout << "Category 2 - Beauty Products" << endl;

cout << "Category 3 - Grocery" << endl;

cout << "Category 4 - Gardening" << endl;

cout << "Category 5 - School supplies" << endl;

cout << "Category 6 - Tobacco products" << endl;

cout << "Choose category(1-6, -1 to exit): ";

cin >> category[n];

if (category[n] == -1)

break;

cout << "Enter price of product: ";

cin >> price[n];

switch (category[n]) {

case 1:

sales_tax += price[n] * 0.06;

break;

case 2:

sales_tax += price[n] * 0.07;

break;

case 3:

sales_tax += price[n] * 0.03;

break;

case 4:

sales_tax += price[n] * 0.06;

break;

case 5:

sales_tax += price[n] * 0.03;

break;

case 6:

sales_tax += price[n] * 0.10;

break;

}

sub_total += price[n];

n++;

}

cout << "\nSub Total: $" << sub_total << endl;

cout << "Sales Tax: $" << sales_tax << endl;

cout << "Total: $" << (sub_total + sales_tax) << endl;

return 0;

}
May 7, 2021 at 7:44am
Please edit for readability.
You missed the end code tag.
https://www.cplusplus.com/articles/jEywvCM9/
May 7, 2021 at 8:51am
As formatted:

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
#include<iostream>
using namespace std;

#define SIZE 100

int main() {
	int category[SIZE];
	double price[SIZE], sales_tax = 0, sub_total = 0;
	int n = 0;

	while (1) {
		cout << "\n\nCategory 1 - Clothing" << endl;
		cout << "Category 2 - Beauty Products" << endl;
		cout << "Category 3 - Grocery" << endl;
		cout << "Category 4 - Gardening" << endl;
		cout << "Category 5 - School supplies" << endl;
		cout << "Category 6 - Tobacco products" << endl;
		cout << "Choose category(1-6, -1 to exit): ";
		cin >> category[n];

		if (category[n] == -1)
			break;

		cout << "Enter price of product: ";
		cin >> price[n];

		switch (category[n]) {
			case 1:
				sales_tax += price[n] * 0.06;
				break;

			case 2:
				sales_tax += price[n] * 0.07;
				break;

			case 3:
				sales_tax += price[n] * 0.03;
				break;

			case 4:
				sales_tax += price[n] * 0.06;
				break;

			case 5:
				sales_tax += price[n] * 0.03;
				break;

			case 6:
				sales_tax += price[n] * 0.10;
				break;
		}

		sub_total += price[n];
		n++;
	}

	cout << "\nSub Total: $" << sub_total << endl;
	cout << "Sales Tax: $" << sales_tax << endl;
	cout << "Total: $" << (sub_total + sales_tax) << endl;
	return 0;
}

May 7, 2021 at 9:12am
There is no need to use arrays - or a switch. Simply:

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
#include<iostream>
using namespace std;

int main() {
	const double tax[6] {0.06, 0.07, 0.03, 0.06, 0.03, 0.10};
	double sales_tax {}, sub_total {};

	for (int n = 0; n != -1; ) {
		cout << "\n\nCategory 1 - Clothing\n";
		cout << "Category 2 - Beauty Products\n";
		cout << "Category 3 - Grocery\n";
		cout << "Category 4 - Gardening\n";
		cout << "Category 5 - School supplies\n";
		cout << "Category 6 - Tobacco products\n";
		cout << "\nChoose category(1-6, -1 to exit): ";

		cin >> n;

		if (n != -1)
			if (n >= 1 && n <= 6) {
				double price {};

				cout << "Enter price of product: ";
				cin >> price;

				sales_tax += price * tax[n - 1];
				sub_total += price;
			} else
				cout << "Invalid category\n";
	}

	cout << "\nSub Total: $" << sub_total << '\n';
	cout << "Sales Tax: $" << sales_tax << '\n';
	cout << "Total: $" << (sub_total + sales_tax) << '\n';
}

Topic archived. No new replies allowed.