Updating Variables from a switch

Hi there! In school, we have to make this kind of simulation of an ATM using C++.

Its almost done but I have one problem.
I have a starting balance of 100,000.
Then, for example, I deposit, 20,000.
After that, I have a new option of making a new transaction with the same account, but the problem is the money in the account resets back to 100,000.
How can I update the balance variable?

P.S.
the account number that works in the code is: 021091 (not real account number)
the pin number that works in the code is: 1234

Thanks in Advance!
javascript:tx('code')
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
#include <iostream>
using namespace std;
int main()
{
	double balance, deposit, withdraw, account, pin;
	int number, choice;
	balance = 100000.00;
	char restart = 'Y' || 'y';

	cout << "Welcome!" << endl;
	cout << endl;
	cout << "Please enter your 6 digit account number: ";
	cin >> account;
	if (account != 21091)
		{
		exit(account != 21091);
	    }
	else
	{
	}
	//The code above will check if your account number is correct
	cout << endl;
	cout << endl;
	cout << "Please enter your 4 digit pin number: ";
	cin >> pin;
	if (pin != 1234)
		{
		exit(pin != 1234);
	    }
	else
	{
	}
	//The code above will check if your pin number is correct
	while (restart == 'Y' || 'y')
	{
	cout << endl;
	cout << endl;
	cout << "============ ATM Main Menu ============";
	cout << endl;
	cout << "[1] Balance Inquiry" << endl;
	cout << "[2] Deposit" << endl;
	cout << "[3] Withdraw" << endl;
	cout << endl;
	cout << "Enter the number of your choice: ";
	cin >> choice;
	cout << endl;
	cout << endl;
	cout << "=======================================" << endl;
	//The code above will give you choices in what you would like to do
	if (choice == 1)
	{
		number = 0;
	}
	else if (choice == 2)
	{
		number = 1;
	}
	else if (choice == 3)
	{
		number = 2;
	}

	else
	{
		number = 3;
	}
	//the code above translates your choice into if else statements so we can assign a number for the switch case code
	switch (number)
	{
	case 0: cout << endl;
	cout << endl;
	cout << "Your current balance is: " << balance << endl;
	cout << "Press any key to continue . . . " << endl;
	cout << "Would you like to make a new transaction?[Y] or [N] : ";
	cin >> restart;
	break;
	//case 0 will appear if you have chosen balance inquiry
	case 1: cout << "Enter your deposit amount: ";
	cin >> deposit;
	cout << endl;
	cout << endl;
	cout << "Your new balance is: " << balance + deposit << endl;
	cout << "Press any key to continue . . . " << endl;
	cout << "Would you like to make a new transaction?[Y] or [N] : ";
	cin >> restart;
	break;
	//case 1 will appear if you have chosen deposit
	case 2: cout << "Enter your desired amount to withdraw: ";
	cin >> withdraw;
	cout << endl;
	cout << endl;
	cout << "Your new balance is: " << balance - withdraw << endl;
	cout << "Press any key to continue . . . " << endl;
	cout << "Would you like to make a new transaction?[Y] or [N] : ";
	cin >> restart;
	break; 
	//case 2 will appear if you have chosen withdraw
	case 3: cout << "ERROR!!" << endl;
	cout << "Please enter a correct corresponding number.";
	cout << "Would you like to make a new transaction?[Y] or [N] : ";
	cin >> restart;
	break; 
	//case 3 will appear if you entered a number that is not in the choices
	}
	
	}

	cin.get();
        return 0;
}
	
Last edited on
cout << "Your new balance is: " << balance + deposit << endl;

This line calculates a new balance, but you haven't actually changed the balance. To change the balance, you need to assign it a new value. For example:

balance = balance + deposit;
Hi Techbask,


First, would it better better to put a comment describing what goes on below
, rather than

 
//The code above will check if your account number is correct 


because we don't read the file backwards. :)

With the following:

1
2
3
4
5
6
7
if (account != 21091)
		{
		exit(account != 21091);
	    }
	else
	{
	}


I can't see the point in the expression passed to the exit function. you might as well have exit(1);
Also it might be nice to do a cout saying that the program is exiting and why.

Now this bit is unneccesary:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (choice == 1)
	{
		number = 0;
	}
	else if (choice == 2)
	{
		number = 1;
	}
	else if (choice == 3)
	{
		number = 2;
	}

	else
	{
		number = 3;
	}


It is better to use choice instead of number as the expression in the switch statement.

There is a problem with this:
 
while (restart == 'Y' || 'y')


Having pointed these things out, you should learn how to debug, either with the debber in your IDE or by using cout statements everywhere to see what your variables are doing.

It's also a good idea to post your compiler output.

Hope this helps.

TheIdeasMan
Yeah, this block is definitely unneccessary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (choice == 1)
	{
		number = 0;
	}
	else if (choice == 2)
	{
		number = 1;
	}
	else if (choice == 3)
	{
		number = 2;
	}

	else
	{
		number = 3;
	}


Instead, use:

1
2
3
4
5
6
7
switch(choice)
{
case 1: ...
case 2: ...
case 3: ...
default: ...
}


The default option will catch if they typed something other than 1, 2, or 3
Topic archived. No new replies allowed.