Using Switch to total the cost

Hey guys, I need a quick lesson and help on why my program isn't working as I want it to. I am practicing using switches and trying to total the cost of items the user buys. I want inputting "4" to exit the switch and go ahead and calculate the total and display the cout(s) I have in my code after the switch. However "4" just ends my program completely. Also inputting "5" just gives me an infinite loop. When I move the loop around and such, I occasionally get an error that tells me that "total" is not initialized. Please help! Thank you! :)

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

int main ()
{

int x, item1, item2, item3;
double subtotal;
subtotal = 0;

item1 = 1599;
item2 = 799;
item3 = 699;


cout << "Guitar Center" << endl;
cout << "__________________________________________________________________________" << endl;
cout << "Item         Guitar              Price          Finish" << endl;
cout << "1.           Gibson Les Paul     $1599          Sunburst" << endl;
cout << "2.           Fender Telecaster   $799           Blonde" << endl;
cout << "3.           Fender Stratocaster $699           Black" << endl;

cout << "Please enter an Item number (1-3) to select a product." << endl;
cin >> x;

do{
switch (x) {
  case 1:
	subtotal = subtotal + item1;
    cout << "You chose 'Gibson Les Paul' in 'Sunburst' finish for '$1599' " << endl;
	cout << "Enter another item or press 4 to checkout" << endl;
	cin >> x;
    break;
  case 2:
	subtotal = subtotal + item2;
    cout << "You chose 'Fender Telecaster' in 'Blonde' finish for $799' " << endl;
	cout << "Enter another item or press 4 to checkout" << endl;
	cin >> x;
    break;
  case 3:
	subtotal = subtotal + item3;
    cout << "You chose 'Fender Stratocaster' in 'Black' finish for $699' " << endl;
	cout << "Enter another item or press 4 to checkout"<< endl;
	cin >> x;
	break;
  case 4:
	  exit(0);
  default:
	  cout << "You entered an invalid Item.";
	  cout << "Please try again";
	  break;
  }
} while (x < '4');



cout << "Your subtotal is $" << subtotal << endl;
double total;
double tax, taxtotal;
tax = 0.07;
taxtotal = (subtotal * tax);
total = (taxtotal + subtotal);

cout << "Your total including tax is $" << total << endl;

system("pause");
return 0;
}
Last edited on
you forgot your break;
or just use goto a;
and then put the label a: wherever you want to go

so the code about case 4 and below should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case 4:
goto a;
break;
 default:
	  cout << "You entered an invalid Item.";
	  cout << "Please try again";
	  break;
  }
} while (x < '4');


a:
cout << "Your subtotal is $" << subtotal << endl;
double total;
double tax, taxtotal;
tax = 0.07;
taxtotal = (subtotal * tax);
total = (taxtotal + subtotal);

cout << "Your total including tax is $" << total << endl;

system("pause");
return 0;
}
Last edited on
Albos,

Thanks, that did the trick! :)

Topic archived. No new replies allowed.