ATM program problem

Hi,

I'm supposed to make a program that mimics an atm. In one part of the program, (if you enter you'd like to withdraw cash) you select to withdraw from checking or savings. The problem is, it won't let you withdraw an amount from one account (that has enough cash) if the other account doesn't have enough. I understand its an error with the if/else statements in the withdrawl function but I don't know what to change to get it to work. I changed this particular line

 
  else if (money > checking || money > savings)


to this:

1
2
3
4
5
6
7
8
9
else if (money > savings)
	{
		cout << "Error, not enough money in savings account" << endl;
	}

	else if (money > checking)
	{
		cout << "Error, not enough money in checking account" << endl;
	}


But that didnt work.

Heres the whole function

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
void ATM::Withdrawl(string transaction, string account, int money)
{
	const int DailyLimit = 300;


	if (money > 0 && money < DailyLimit && money < savings && money < checking)
	{

		if (account == "Savings")
		{
			wAmount += money;
			savings -= money;
		}
		else if (account == "Checking")
		{
			wAmount += money;
			checking -= money;
		}
		PrintReceipt(transaction, account, money);
	}
	else if (money < 0)
	{
		cout << "Error, must enter an amount above 0" << endl;
	}
	else if (money > DailyLimit)
	{
		cout << "Error, amount exceeds daily limit" << endl;
	}
	else if (money > savings)
	{
		cout << "Error, not enough money in savings account" << endl;
	}

	else if (money > checking)
	{
		cout << "Error, not enough money in checking account" << endl;
	}
	
}


If anyone could help that would be greatly appreciated
it looks like the problem is on line 7

1
2
3
if (money > 0 && money < DailyLimit && money < savings && money < checking)
//money < savings || money<checking  


let me know if this fixes you problem
Last edited on
Ok, that fixed that, but now it accepts negative balances if I withdraw more than whats in the account
If they're withdrawing from a checking account, sounds like you don't want to check the savings account at all, and vice versa.

if the withdrawal amount is invalid (<0 or >daily limit - regardless of account type?)
--- error message
else if they want to withdraw from checking
--- do they have the money available in checking?
--- if yes, process withdrawal, if no, error message
else if they want to withdraw from savings
--- do they have the money available in savings?
--- if yes, process withdrawal, if no, error message
Last edited on
Thanks wildblue, I think that covers it all. Thanks for setting up your reply to get me to understand it rather than just giving me the answer. It helps a lot.
Another error I found: if an invalid choice is selected, it displays the error message but carries on as if a valid choice was chosen. I'll present my entire code:

Client file
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

#include <iostream>
#include <string>
#include "atm.h"

using namespace std;
// prototypes for the input functions that will go in the client file
string choosetransaction();
string chooseaccount();
int getmoney();


int main()
{	// Purpose:  The purpose of this main program is to create one object called Henry that will process his ATM transactions.

	// variable section
	string  transaction;
	string account;
	int money;
	ATM Henry(500, -500);		//Instantiation of the only class object used in this program

	cout << "Welcome to the CS2020 ATM\n";
	cout << "----------------------------------------\n";
	transaction = choosetransaction();
	while (transaction != "Quit")
	{
		account = chooseaccount();
		money = getmoney();
		if (transaction == "Deposit")
			Henry.Deposit(transaction, account, money);
		else if (transaction == "Withdrawal")
			Henry.Withdrawl(transaction, account, money);
		else if (transaction == "Transfer")
			Henry.transferTo(transaction, account, money);
		transaction = choosetransaction();
	}
	return 0;
}// main
//Complete the input function by adding the correct lopp validation conditions below

//--------------------------------------------------------------------------
// This is the chosetransaction to retrieve the transaction from the user
// The function has no parameter list and returns the valid string
// version of the transaction. This function also validate the input from the
// keyboard.
//---------------------------------------------------------------------------
string choosetransaction()
{
	string trans;

	cout << "----------------------------------------\n";
	cout << "(D)  Deposit\n"
		<< "(W)  Withdrawal\n"
		<< "(T)  Transfer To\n"
		<< "(Q)  Quit\n";
	cout << "----------------------------------------\n"
		<< "Make a selection : ";
	cin >> trans;
	trans = toupper(trans[0]); // converts lowercase to uppercase
	while (trans != "D" || trans != "W" || trans!= "T" || trans!= "Q" )
	{
	cout << trans << endl;
		cout << "----------------------------------------\n";
		if (trans == "D")
			trans = "Deposit";
		else if (trans == "W")
			trans = "Withdrawal";
		else if (trans == "T")
			trans = "Transfer";
		else if (trans == "Q")
			trans = "Quit";

		return trans;
	}

}
//--------------------------------------------------------------------------
// This is the choseaccount to retrieve the account from the user
// The function has no parameter list and returns the valid string
// version of the account. This function also validate the input from the
// keyboard.
//--------------------------------------------------------------------------
string chooseaccount()
{
	string account;

	cout << "----------------------------------------\n";
	cout << "(C) Checking Account\n"
		<< "(S) Savings Account\n";
	cout << "----------------------------------------\n"
		<< "Make a selection : ";
	cin >> account;
	cout << account << endl;
		account = toupper(account[0]); //converts lowercase to uppercase
	while (account!="C" || account!="S")
	{
		cout << "----------------------------------------\n";
		if (account == "C")
			account = "Checking";
		else if (account == "S")
			account = "Savings";
		

		return account;
	}
	


	
}
//--------------------------------------------------------------------------
// This is the getmoney to retrieve the monetary value from the user
// The function has no parameter list and returns the valid monetary
// value. This function also validate the input from the
// keyboard. The value must be a positive multiple of 10.
//--------------------------------------------------------------------------
int getmoney()
{
	int money;
	cout << "Please enter the amount of money (multiple of 10s) : ";
	cin >> money;
	cout << money << endl;
		while (money<0 || money%10 != 0)
		{
		cout << "The amount must be non-negative "
			<< "and a multiple of 10.\n"
			<< "Please try again : \n";
		cout << "Please enter the amount of money (multiple of 10s) : ";

		cin >> money;
		cout << money << endl;
		}
	return money;
}


Header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>

using namespace std;

class ATM{
private:
	int savings;
	int checking;
	int wAmount;
	void PrintReceipt(string transaction, string account, int money);
public:
	ATM();
	ATM(int savings, int checking);
	void Deposit(string transaction, string account, int money);
	void Withdrawl(string transaction, string account, int money);
	void transferTo(string transaction, string account, int money);

};


Implementation
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
#include <iostream>
#include <iomanip>
#include "atm.h"

using namespace std;

ATM::ATM()
{
	savings = 0;
	checking = 0;

}

ATM::ATM(int inputsavings, int inputchecking)
{
	if (inputsavings < 0)
	{
		savings = 0;
	}
	else
	{
		savings = inputsavings;
	}
	if (inputchecking < 0)
	{
		checking = 0;
	}
	else
	{
		checking = inputchecking;
	}

	wAmount = 0;
}

void ATM::Deposit(string transaction, string account, int money)
{
	if (account == "Savings")
	{
		savings += money;
	}
	else if (account == "Checking")
	{
		checking += money;
	}
	PrintReceipt(transaction, account, money);
}
void ATM::Withdrawl(string transaction, string account, int money)
{
	const int DailyLimit = 300;


	if (money > 0 && money < DailyLimit)
	{

		if (account == "Savings")
		{
			if (money < savings)
			{
				wAmount += money;
				savings -= money;
			}
			else
			{
				cout << "Error, not enough money in savings" << endl;
			}
				
		}
		else if (account == "Checking")
		{
			if (money < checking)
			{
				wAmount += money;
				checking -= money;
			}
			else
			{
				cout << "Error, not enough money in checking" << endl;
			}
		}
	}
	else if (money < 0)
	{
		cout << "Error, must enter an amount above 0" << endl;
	}
	else if (money > DailyLimit)
	{
		cout << "Error, amount exceeds daily limit" << endl;
	}	
	PrintReceipt(transaction, account, money);
}

void ATM::transferTo(string transaction, string account, int money)
{
	if (savings > money && checking > money)
	{
		if (account == "Savings")
		{
			if (money < savings)
			{
				savings += money;
				checking -= money;
			}
			else
			{
				cout << "Error, not enough money in savings" << endl;

			}
		}
		else if (account == "Checking")
		{
			if (money < checking)
			{
				savings += money;
				checking -= money;
			}
			else
			{
				cout << "Error, not enough money in checking" << endl;

			}
		}
		PrintReceipt(transaction, account, money);
	}
}

void ATM::PrintReceipt(string transaction, string account, int money)
{ 
	cout << endl;
	cout << "There is now $" << savings << " in savings" << endl;
	cout << "There is now $" << checking << " in checking" << endl;
}
Topic archived. No new replies allowed.