Basic c++ program problem

This program is supposed to act as an ATM, but the problem I'm running into is whenever the user inputs an answer, it only goes to withdrawal, and isn't working for any of the other options. Please help!

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

void choices(int &answer);
void withdrawl(int &answer,int &withdrawl_ans, double &c_balance, double &s_balance);
void deposit(int &answer, int &deposit_ans, double &c_balance, double &s_balance);
void transfer(int &answer, int &transfer_ans, double &c_balance, double &s_balance);
void end_program(int &answer);


int main(){
    int pin, answer, withdrawl_ans, deposit_ans, transfer_ans;
    double s_balance = 100;
    double c_balance = 100;
    
    cout << "Please enter your pin: ";
    
    do{
        cin >> pin;
        if(pin != 9009){
            cout << "Invalid Pin. Please try again: ";
        }
    }while(pin != 9009);
    
    choices(answer);
    withdrawl(answer, withdrawl_ans, s_balance,c_balance);
    deposit(answer, deposit_ans, c_balance, s_balance);
    transfer(answer, transfer_ans, c_balance, s_balance);
    end_program(answer);
    
    return (0);
}
void choices(int &answer){
    cout << "1. Withdrawal" << endl;
    cout << "2. Deposit" << endl;
    cout << "3. Check Balances" << endl;
    cout << "4. Transfer" << endl;
    cout << "5. Quit" << endl;
    cout << "Enter the number of the option you would like: ";
    cin >> answer;
    
}

void withdrawl(int &answer,int &withdrawl_ans, double &c_balance, double &s_balance){
    int withdrawl_savings_amount;
    int withdrawl_checking_amount;
    
    if(answer == 1){
        cout << "1. Checking" << endl;
        cout << "2. Savings" << endl;
        cout << "3. Cancel" << endl;
    }
    cout << "Enter the number of the option you would like: ";
    
    cin >> withdrawl_ans;
    
    if(withdrawl_ans == 1){
        cout << "In increments of $5.00, how much would you like to withdrawl \nfrom your checking: ";
        cin >> withdrawl_checking_amount;
        c_balance -= withdrawl_checking_amount;
        choices(answer);
        }
    if(withdrawl_ans == 2){
        cout << "In increments of $5.00, how much would you like to withdrawl \nfrom your savings: ";
        cin >> withdrawl_savings_amount;
        s_balance -= withdrawl_savings_amount;
        choices(answer);
    }
    if(withdrawl_ans == 3){
        choices(answer);
    }
        
}
void deposit(int &answer, int &deposit_ans, double &c_balance, double &s_balance){
    int deposit_savings_amount;
    int deposit_checking_amount;
    if(answer == 2){
    
        cout << "1. Checking" << endl;
        cout << "2. Savings" << endl;
        cout << "3. Cancel" << endl;
    }
    cout << "Enter the number of the option you would like: ";
    
    cin >> deposit_ans;
    
    if(deposit_ans == 1){
        cout << "Enter deposit amount to your checking: ";
        cin >> deposit_checking_amount;
        c_balance += deposit_checking_amount;
        choices(answer);
    }
    if(deposit_ans == 2){
        cout << "Enter deposit amount to your savings: ";
        cin >> deposit_savings_amount;
        s_balance += deposit_savings_amount;
    }
    if(deposit_ans == 3){
        choices(answer);
    }
    
    
}
void check_balances(int &answer, double &c_balance, double &s_balance){
    if(answer == 3){
        cout << "The balance of your checking account is: " << c_balance << endl;
        cout << "The balance of your savings account is: " << s_balance << endl;
    }
    choices(answer);
}
void transfer(int &answer, int &transfer_ans, double &c_balance, double &s_balance){
    double transfer_amount;
    if(answer == 4){
        cout << "1. From Checking " << endl;
        cout << "2. From Savings " << endl;
        cout << "3. Cancel" << endl;
    }
    cin >> transfer_ans;
    if(transfer_ans == 1){
        cout << "How much would you like to transfer to your savings: ";
        cin >> transfer_amount;
        c_balance -= transfer_amount;
        s_balance += transfer_amount;
        cout << "Your new savings balance is $" << s_balance << endl;
        cout << "Your new checking balance is $" << c_balance << endl;
        
        choices(answer);
    }
    if(transfer_ans == 2){
        cout << "How much would you like to transfer to your checking: ";
        cin >> transfer_amount;
        s_balance -= transfer_amount;
        c_balance += transfer_amount;
        cout << "Your new checking balance is $" << c_balance << endl;
        cout << "Your new savings balance is $" << s_balance << endl;
        
        choices(answer);
    }
    if(transfer_ans == 3){
        choices(answer);
    }
}
void end_program(int &answer){
    if(answer == 5){
        exit(1);
    }
}






On line 29 whatever answer is entered you are always executing all
1
2
3
4
   withdrawl(answer, withdrawl_ans, s_balance,c_balance);
   deposit(answer, deposit_ans, c_balance, s_balance);
   transfer(answer, transfer_ans, c_balance, s_balance);
   end_program(answer);

these operations in a row. There are no switch or if statements that would determine what operations should be used based on given answer. You can enter INT_MIN ... INT_MAX and next operations will still always be withdrawl followed by deposit and so on
Last edited on
Okay, so if I use if statements for each one of the function calls, such as if(answer == 1){
withdrawl(answer, withdrawl_ans, s_balance, c_balance);
}
and for the other ones, I get an error saying answer is undeclared.

Actually, I think I fixed it. I got rid of the choices function, and moved it to the main. How to I get back to the choices menu though after I go about withdrawing, depositing, checking the balance, etc?
You can put the part that asks for action in the loop and than execute the action what user wanted. After the action is executed you will return to the start of the loop and the question about what to do will be asked again. Only quit the loop if user wants to exit like

1
2
3
4
5
6
7
8
9
10
11
12
bool done = false;

while(!done){
    void choices(answer);

    switch(answer){
    case 1 : {/*do one action*/ break;}
    case 2 : {/*do another action*/ break;}
    //...
    case 5 : done = true; /*Exit from program ... now loop wont repeat itself*/
    }
}
Topic archived. No new replies allowed.