ATM machine program help

Soo, this was an in class assignment I had to do for my class, where I had to write something resembling an ATM machine. The account is supposed to start with $500 and when I withdraw, I'm supposed to make it so that if the user does not enter a multiple of 20, it will decline and ask again, but I couldn't get that part right..

This was what I was graded on (Did not do well at all! :( )
Functions are set up properly (prototype and definition) 2/4
Functions are programmed properly 2/4
Functions are called properly 2/4
Balance updates deposited amount 0/1
Program loops properly 1/1
Prompt user for menu choice 1/1
Verify choice 1/1
Sentinel Value is set up, used, and displayed properly 1/1
Program prints out withdrawn amount (loop) 0/1

Can anyone help me figure out how I would do the withdrawals in multiples of 20? Also with other mistakes where I got points from my grade deducted? I don't really understand...didn't think I'd get that low of a grade. :/

Thanks, all help appreciated!

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

using namespace std;

double deposit(double, double);
double withdraw(double,double);

int main()
{
	int choice=0;
	double value1, value2, result;
	do
	{
		cout << "(1) Deposit" << endl;
		cout << "(2) Withdraw" << endl;
		cout << "(-1) Exit" << endl;
		cout << "Choose an option: " << endl;
		cin >> choice;


		if(choice == 1)
		{
			cout << "How much money would you like to deposit?" << endl;
			cin >> value1;
		}
        if(choice == 2)
        {
		    cout << "How much money would you like to withdraw?" << endl;
			cin >> value2;
        }	
		
		if(choice ==1)
		{
			result = deposit(value1,value2);
		}
		else if(choice==2)
		{
			while(value2 ==20.0)
			{
				cout << "You can only withdraw in increments of 20." << endl;
				cin >> value2;
			}
			result = withdraw(value1,value2);
		}
		else if(choice ==-1)
		{
			//Exit Menu
		}
		else
		{
			cout << "Please enter a valid option \n" << endl;
		}
		if(choice >0 && choice <3)
		{
			cout << "You have $" << result << " left in your bank account" << endl;
		}
	}while(choice != -1); 
      
	system("pause");
	return 0;
}

double deposit(double num1, double num2)
{
	return (500 + num1);
}
double withdraw(double num1, double num2)
{
       
    return(500 - num2);
}
Last edited on
Can anyone help me figure out how I would do the withdrawals in multiples of 20?

The modulus operator springs to mind;

1
2
3
4
5
while ((value2 % 20) != 0)
			{
				cout << "You can only withdraw in increments of 20." << endl;
				cin >> value2;
			}


Other comments;

- your function prototypes could have had meaningful variable names in them:
double deposit(double amountToDeposit);, for example.

- your functions both accepted two parameters, but only actually used one of them

- your variables could have had better names
double amountToDeposit, amountToWithdraw for example

- Why double? This being an ATM, the int type is begging to be used.

- system("pause"); is expensive, dangerous and non-portable.

- You don't keep track of the total amount. If a user deposits 100, and then deposits 100 again, and then again, and again, every time they will be told they have 600 in the account.

- Looks like the mark scheme expected some kind of on-screen confirmation of the amount the user withdrew, not just how much was left.

- Your code layout is not so neat as it could (and should) be. Instead of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	if(choice == 1)
		{
			cout << "How much money would you like to deposit?" << endl;
			cin >> value1;
		}
        if(choice == 2)
        {
		    cout << "How much money would you like to withdraw?" << endl;
			cin >> value2;
        }	
		
		if(choice ==1)
		{
			result = deposit(value1,value2);
		}
		else if(choice==2)
		{
			while(value2 ==20.0)
			{
				cout << "You can only withdraw in increments of 20." << endl;
				cin >> value2;
			}
			result = withdraw(value1,value2);
		}


this would have been better
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(choice == 1)
		{
			cout << "How much money would you like to deposit?" << endl;
			cin >> value1;
                        result = deposit(value1,value2);
		}
if(choice == 2)
                {
		         cout << "How much money would you like to withdraw?" << endl;
			 cin >> value2;
                         while(value2 ==20.0)
			  {
				cout << "You can only withdraw in increments of 20." << endl;
				cin >> value2;
			  }
			  result = withdraw(value1,value2);
                 }


- This is completely useless, does nothing except clutter up the code
1
2
3
4
else if(choice ==-1)
		{
			//Exit Menu
		}

I'm guessing it's there purely to stop this code being hit:
1
2
3
4
else
		{
			cout << "Please enter a valid option \n" << endl;
		}

There are ways to do this without having a do-nothing branch.

- It seems that you only get to find out how much is in the account if you enter a value greater than zero or less than 3. In expected operation, this is when someone has chosen 1 or 2, and AFTER they've withdrawn/deposited. It would have been perhaps more sensible for an ATM to tell the user how much money they have before they withdraw/deposit.
1
2
3
4
if(choice >0 && choice <3)
		{
			cout << "You have $" << result << " left in your bank account" << endl;
		}


- Could have done with some handling of what happens if the user wants to withdraw more than they have in the account.

Last edited on
@Moschops

Thanks for the quick reply!

If the user enters a number that is not 1, 2, or -1, it does prompt the user to enter a valid input, but I just noticed that if I put in a letter, it enters an infinite loop. Oops! I also fixed my variable names and changed my doubles to ints. Thanks a lot!

I think my biggest problem with the program though that I didn't catch before was that I did not update/keep track of the total amount after a withdrawal/deposit. Would you be able to guide me in the direction of how I would go about making that work?

Thanks again, you've no idea how much I appreciate your help!
Last edited on
int existingBalance=500; at the start, and functions like:

1
2
3
4
void deposit(int& existingBalance, double amountDeposited)
{
	existingBalance = existingBalance + amountDeposited;
}


or if you're not happy with calling by reference,

1
2
3
4
5
int deposit(int existingBalance, double amountDeposited)
{
  int newBalance = existingBalance + amountDeposited;
  return newBalance;
}
Last edited on
To handle unwanted input, have a check at the start:

1
2
3
4
5
6
// Cout code at start of loop
cin >> choice;
if (choice != 1 || choice != 2 || choice != -1) { // If wrong input
    cout << "Invalid choice!\n";  // Warn user
    continue;  // Restart loop
}

There are infinite options of input. Rather than checking all for all possibilities, just check the ones you accept. Rather than check if it equals what you want, it's often easier to check if it's not one of the values you want. Thanks to this check, you can be sure that there won't be unexpected input and all the rest of the code in your loop can safely assume that the input is a correct value.

Since your question is at the beginning, the "continue;" keyword is golden. Rather than retyping code to ask again in case of wrong input, just skip the rest of the loop body. You can use this after any check, if you're lazy, but that's not always the best scenario.

Check for multiples of 20, for example. Why do you wait? Do it immediately after asking. The input's not going to change. Now, to handle it, you have two options. The easy way: if it's not 20, call "continue;" and restart the loop so the user has to input his choice again. The fancier way would be to have an extra loop inside it that re-asks the amount until the input is correct. (Also, make sure to provide sufficient information when asking input. If you need it to be a multiple of 20, say so!).

Your program isn't keeping track of the result because you do "500-num1". Instead, have a variable "total" and do "total-num1" when you withdraw. Initialize it at 500 at the start of your program.
Last edited on
Topic archived. No new replies allowed.