Help with switching program

hi guys,

new to programming and i am a week behind our tutorials. i need to get 3 other qus done for last week.

Basicall i have to assume the processing of an ATM, using switches. I have made the menu so far but am having trouble getting the input from the user.

I have to assume there is already a bank account and therefore must be a variable for the balance in the account.

With the switch statement the program must:
- output the balance on the screen
- let the user add (deposit) an amount to the existing balance
- let the user subtract (withdraw) an amount from the balance

I have made the menu and i have made a default so if anything else is entered the user will be informed of an error.I have also started putting together the forumla for calculating the balance if when a user inputs a deposit or withdrawl amount.
Everytime you choose an option from the menu i get the default??

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

using namespace std;

int main()

{
    
    int option, dep, balance, wth;
    
    cout << "Please choose from the following: " << endl;
    cout << "B - Show balance. D - Deposit. W - Withdrawal. " << endl;
    cin >> option;
    
        switch (option)
    {
           
    case 'B':
         cout << "B. Show balance. ";
         cout << "your balance is: $ " << balance;        
         break;
         
         
         
    case 'D':
         cout << "D. Deposit. ";
         cin >> dep;
         balance = balance + dep;
         cout << "You have deposited: " << dep << " dollars";
         break;
         
         
        
    case 'W':
         cout << "W. Withdrawal. ";
         cin >> wth;
         balance = balance - wth;
         cout << "you have withdrawn: " << wth << " dollars";
         break;
         
    default:
        cout << "invalid option." << endl;
                
    }
    
    system ("pause");
    
    return 0;
    
}


When checking the input it is case sensitive, so if you put in 'b' it will be different from 'B' and hence go to the default.

It could also be that you are reading into a int which might not like it if you input characters. Use a char instead.
thanks yea char lets the menu work :) how would do i make the program loop back to the menu after a switch?

also is there a way for the program's switches to not be case sensitive? if not nevermind.

cheers!!
Last edited on
also is there a way for the program's switches to not be case sensitive? if not nevermind.


You could either force the character to be a specific case using toupper or tolower, or just check both cases:

1
2
3
case 'B':
case 'b':
//code 
i see. cheers.

any ideas how i can get the switch to get back to the menu instead of breaking then exiting?
Use a while loop or something similar.
Topic archived. No new replies allowed.