Programming Assignment Help

I'm having trouble with my C++ programming assignment.
A mail order company sells 3 products, A, B, and C. The prices for these products are $1.99, $2.99, and $3.99. Write a program to read the product type and the quantity sold. Use a switch statement to determine the total sales for each transaction. Use a sentinel controlled loop to determine when the program should stop and display the final results.

I keep getting an error message: "The variable 'charges' is being used without being initialized."

So far I have:

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

int main()
{
const int A = 1, B = 2, C = 3, Z = 4;
const int A_PRODUCT = 1, B_PRODUCT = 2, C_PRODUCT = 3, QUIT_PRODUCT = 4;
const double PRODUCT_A = 1.99, PRODUCT_B = 2.99, PRODUCT_C = 3.99;
int product;
int quantity;
double charges;

cout << setprecision(2) << fixed << showpoint;

cout << "Enter product type and quantity sold (enter Z to stop): ";
cin >> product >> quantity;


while (product != QUIT_PRODUCT)
{
cout << "Enter product type and quantity sold (enter Z to stop): ";
cin >> product >> quantity;

switch (product)
{
case A_PRODUCT:
charges = quantity * PRODUCT_A;
break;
case B_PRODUCT:
charges = quantity * PRODUCT_B;
break;
case C_PRODUCT:
charges = quantity * PRODUCT_C;
break;
}

cout << "Total sales of all products = " << charges << "\n";
} while (product != QUIT_PRODUCT)
return 0;
}
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) It's possible for your switch statement to fall through with none of the cases being hit. This means that, when you output the value of charges, no value will ever have been assigned to it.

As a general rule, it's sensible to initialise your variables with a sensible default.
Your prompts say to enter Z to stop, but you're actually checking for 4 (QUIT_PRODUCT).

You should be getting syntax errors on your while statements. You appear have the following
1
2
3
4
 while (condition)
{ // statements
}
while (condition)


You can have do/while or just while, but not while/while.
I have updated the code and now have the following:

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

int main()
{
	char product;
	int quantity;
	float chargeA, chargeB, chargeC;
	const double PRODUCT_A = 1.99, PRODUCT_B = 2.99, PRODUCT_C = 3.99;

	cout << setprecision(2) << fixed << showpoint;

	cout << "Enter product type and quantity sold (enter Z to stop): ";
	cin >> product >> quantity;

	while (product != 'Z')
	{
		cout << "Enter product type and quantity sold (enter Z to stop): ";
		cin >> product >> quantity;

		switch (product)
		{
			case 'A':
				cout << "Enter product type and quantity sold (enter Z to stop): ";
				cin >> product >> quantity;
				chargeA = quantity * PRODUCT_A;
				break;
			case 'B':
				cout << "Enter product type and quantity sold (enter Z to stop): ";
				cin >> product >> quantity;
				chargeB = quantity * PRODUCT_B;
				break;
			case 'C':
				cout << "Enter product type and quantity sold (enter Z to stop): ";
				cin >> product >> quantity;
				chargeC = quantity * PRODUCT_C;
				break;
		}
			
	}
	cout << "Total sales of product A = $" << chargeA << "\n";
	cout << "Total sales of product B = $" << chargeB << "\n";
	cout << "Total sales of product C = $" << chargeC << "\n";
	cout << "Total sales of all products =$" << chargeA + chargeB + chargeC << "\n";
	return 0;
} 


For some reason the Total sales of products A, B, C, and all products are not displaying after I hit Z.

By the way thank you for the help so far!
Here's a thought

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

int main()
{
	char product;
	int quantity;
	float chargeA, chargeB, chargeC;
	const double PRODUCT_A = 1.99, PRODUCT_B = 2.99, PRODUCT_C = 3.99;

	cout << setprecision(2) << fixed << showpoint;

	do
	{
		cout << "Enter product type (enter Z to stop): ";
		cin >> product;
		product = toupper(product);
		if(product != 'Z')
		{
		    cout << "\nEnter an amount: ";
		    cin >> quantity;

    		switch (product)
    		{
    			case 'A':
    				chargeA = quantity * PRODUCT_A;
    				break;
    			case 'B':
    				chargeB = quantity * PRODUCT_B;
    				break;
    			case 'C':
    				chargeC = quantity * PRODUCT_C;
    				break;
    		}
		}
			
	} while (product != 'Z');
	
	cout << "Total sales of product A = $" << chargeA << "\n";
	cout << "Total sales of product B = $" << chargeB << "\n";
	cout << "Total sales of product C = $" << chargeC << "\n";
	cout << "Total sales of all products =$" << chargeA + chargeB + chargeC << "\n";
	return 0;
} 
After a few minor adjustments to your code, I finally got it to work properly. Thank you so much!
Topic archived. No new replies allowed.