Switch Statement Problem

Hey everyone,

So I'm trying to write a sort of piggybank code for a school project. It's supposed to let you deposit, withdraw, and see what coins you have.

My professor wants us to use switch statements, and I thought I had done them correctly, but now, the program isn't working.

Also, the program is not supposed to accept negative values when depositing/withdrawing, yet it still does. Can anyone help?

I was wondering if anyone could provide some insight as to why this isn't letting me input an option (1-4).

My code is as follows:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# include <iostream>
# include <iomanip>

using namespace std;

int main()
{
	float balance;
	double dep_amt, dep_max, wdrw_amt, remainder;
	int num_quart, num_dime, num_nickel, num_penny;
	int choice;

	cout << setprecision(2) << fixed;

	balance = 0.00;

	do
	{
	cout.fill('*');
	cout << setw(35) << '*' << endl;
	cout << "*       Piggy Bank Menu           *" << endl;
	cout << "*                                 *" << endl;
	cout << "*    The balance is: $ " << balance << setw(6) << "       *" << endl;
	cout << setw(35) << '*' << endl;
	cout << "*                                 *" << endl;
	cout << "* 1)  Make Deposit                *" << endl;
	cout << "* 2)  Make Widthdrawal            *" << endl;
	cout << "* 3)  View Coins                  *" << endl;
	cout << "* 4)  Exit Program                *" << endl;
	cout << "*                                 *" << endl;
	cout << setw(35) << '*' << endl;
	cout << "Enter choice: ";
	cin >> choice;

	switch (choice)
	{
	case '1' :  cout << "Enter amount to be deposited: $";
				cin >> dep_amt;
				if (dep_amt < 0)
				{
					cout << "Invalid amount. Negative values are not allowed." << endl;
				}
				if (dep_amt + balance > 900)
				{
					dep_max = 900 - balance;
					cout << "The piggybank will not hold that much.";
					cout << "You may deposit up to $" << dep_max << endl;
				}
				break;

	case '2'  :  cout << "Enter amount to be withdrawn: $";
				cin >> wdrw_amt;
				if (wdrw_amt < 0)
				{
					cout << "Invalid amount -- Negative values are not allowed." << endl;
				}
				if (wdrw_amt > balance)
				{
					cout << "The piggybank does not contain that much." << endl;
					cout << "You may withdraw up to $" << balance;
				}
				balance -= wdrw_amt;
				break;

	case '3' :
				num_quart = balance / 0.25;
				remainder = balance - num_quart * 0.25;
				num_dime = remainder / 0.10;
				remainder = remainder - num_dime * 0.10;
				num_nickel = remainder / 0.05;
				remainder = remainder - num_nickel * 0.05;
				num_penny = remainder / 0.01;

				cout << setw(35) << '*' << endl;
				cout << "*            Statistics           *" << endl;
				cout << setw(35) << '*' << endl;
				cout << "Quarters: " << num_quart;
				cout << " $ " << num_quart * 0.25 << endl;
				cout << "Dimes: " << num_dime;
				cout << " $ " << num_dime * 0.10 << endl;
				cout << "Nickels: " << num_nickel;
				cout << " $ " << num_nickel * 0.05 << endl;
				cout << "Pennies: " << num_penny;
				cout << " $ " << num_penny * 0.01 << endl;
				break;

	case '4' :  cout << "GOODBYE!!";
				return 0;
				break;

	default :   cout << "Invalid option. Try again."<< endl;
				break;
	}
		} while (choice >= 1 || choice <= 3);

	return 0;
}




Any help is appreciated. Thanks!
Last edited on
your case labels are char so choice should be char
Wow, I can't believe I missed that.

Thank you!
Is there anything you can suggest I do about the negative values problem?
Topic archived. No new replies allowed.