ATM Machine - issue storing balance

I'm working on a c++ ATM machine project, and I've run into an issue storing the balance amount beyond a single transaction. When the code runs, each iteration of a transaction reverts back to balance = 0.

Example: Transaction, deposit $ 5.00, balance = $5.00. New transaction, withrdaw $2.00, balance = $ - 2.00 (instead of $3.00).

Can anyone point me in the right direction? Thanks in advance.


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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

#include "stdafx.h"
#include "user.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>

using namespace std;

//function prototypes
void createAccount(string userN, string userP, int &nou, vector<user> &userList);
int login(string uName, string uPass, vector<user> userList);
void deposit(string deposit, int unumber, vector<user> userList);
void withdraw(string withdraw, int unumber, vector<user> userList);
void getBalance(char lastTransaction, string withdraw, string deposit, int unumber, vector<user> userList);
void checkingsTotal();
void savingsTotal();


int main()
{
	char userSelection=0;
	char lastTransaction;
	string userN, userP;
	vector<user> userList;

	int numberOfUsers = 0;
	int unumber;
	string deposit2, withdraw1;
	userList.resize(10);

	while (userSelection != 'q') {
		cout << "Welcome to COP2513.F16's ATM Machine" << endl;
		cout << "Please select an option: " << endl;
		cout << "l -> Login" << endl;
		cout << "c -> Create New Account" << endl;
		cout << "q -> Quit" << endl;
		cout << ">";


		cin >> userSelection;


		if (userSelection == 'l') {
			cout << "Please enter your user id: ";
			cin >> userN;
			cout << "Please enter your password: ";
			cin >> userP;
			
			unumber = login(userN, userP, userList);						//calls login with username password and passes the vector.
			if ((unumber >= 0)) {											//if the return value is >= 0 the user has been found in the
				cout << "Access Granted - " << userN << endl;				//list and the password matched so user has logged in
				cout << "Please select an option: " << endl;				
				do {
					cout << "d -> Deposit Money\nw -> Withdraw Money\nr -> Request Balance\nx -> Exit\n";  
					cin >> userSelection;		

					//switch to handle logged in users requests									
					switch (userSelection) {
					case 'w': {
						cout << "Amount of withdraw: ";
						lastTransaction = 'w';
						cin >> withdraw1;
						withdraw(withdraw1, unumber, userList);
						break;
					}
					case 'r': { getBalance(lastTransaction, withdraw1, deposit2, unumber, userList);
						break;
					}
					case 'd': {
						cout << "Amount of deposit: ";
						lastTransaction = 'd';
						cin >> deposit2;
						deposit(deposit2, unumber, userList);
						break;
					}
					case 'x': {
						cout << "Thanks for banking with COP2513.F16, " << userN << " " << endl;
					}
					default:
						cout << "Command not recognized.\n";

					}
				} while (userSelection != 'x');
			}

			else {
				cout << "******** LOGIN FAILED! ********" << endl;
			}

		}


		else if (userSelection == 'c') {

			cout << "Please enter your user name: ";
			cin >> userN;

			cout << "Please enter your password: ";
			cin >> userP;
			createAccount(userN, userP, numberOfUsers, userList);
			cout << "Thank you! Your account has been created! " << endl;
			numberOfUsers++;
		}

		else if (userSelection == 'q') {
			cout << "Thanks for banking with COP2513.F16, " << userN << " " << endl;
		}

		else {
			cout << "Command " << userSelection << "not recognized.";
		}

	}
	return 0;
}

int login(string uName, string uPass, vector<user> userList) {
	//searches the vector for user name. is the name exists it checks the password
	//if the password is correct it returns true; otherwise returns false
	for (unsigned int i = 0; i < userList.size(); i++) {
		if (userList[i].getUsername().compare(uName) == 0 && userList[i].getUserPassword().compare(uPass) == 0) {
			return i;

		}
	}
	return -1;
}
void getBalance(char lastTransaction, string withdraw, string deposit, int unumber, vector<user> userList) {
	//if last transaction was withdraw
	if (lastTransaction == 'w') {
		printf("Beginning balance: %f\n", (userList[unumber].getBalance()));
		cout << "Withdraw amount: $" << withdraw << endl;
		printf("Your balance: %f\n", userList[unumber].getBalance() - stod(withdraw));
	}
	//if last transaction was deposit
	else if (lastTransaction == 'd') {
		printf("Beginning balance: $%f\n", (userList[unumber].getBalance()));
		cout <<"Deposit amount: $" << deposit <<endl;
		printf("Your balance is : $%f\n", userList[unumber].getBalance() + stod(deposit));
	}
	//if no transactions
	else {
		printf("Your balance is : $%f \n", userList[unumber].getBalance());
	}
}
void withdraw(string withdraw, int unumber, vector<user> userList) {
	double tempAmount;
	withdraw.erase(std::remove(withdraw.begin(), withdraw.end(), '$'), withdraw.end()); //removes $ from string
	withdraw.erase(std::remove(withdraw.begin(), withdraw.end(), ','), withdraw.end()); //removes , from string
	tempAmount = stod(withdraw);														//cast withdraw string to double 
	userList[unumber].modifyBalance((userList[unumber].getBalance()) - tempAmount);		 //withdraws money from user account
	


}
void deposit(string deposit, int unumber, vector<user> userList) {
	double tempAmount;
	deposit.erase(std::remove(deposit.begin(), deposit.end(), '$'), deposit.end()); //removes $ from string
	deposit.erase(std::remove(deposit.begin(), deposit.end(), ','), deposit.end()); //removes , from string
	tempAmount = stod(deposit);														//cast deposit string to double 
	userList[unumber].modifyBalance(userList[unumber].getBalance() + tempAmount);	//withdraws money from user account

}
void createAccount(string userN, string userP, int &nou, vector<user>  &userList) {
	if (nou == userList.size()) userList.resize(5); //resize vector if array is full
													//set values of new user
	userList[nou].setUsername(userN);
	userList[nou].setUserpassword(userP);
	userList[nou].modifyBalance(0);
	nou++;



}


You haven't posted the body of your "user::modifyBalance" function, but I'm guessing it looks like this:
1
2
3
void user::modifyBalance(double new_balance) {
	balance = new_balance;
}



Let's look at line 155 (or 165) :
userList[unumber].modifyBalance((userList[unumber].getBalance()) - tempAmount);

"getBalance()" doesn't return anything. Its return type is void, and it looks like all it does is print some stuff. You're treating it like it should be returning the current balance, so that you can subtract (in the case of a withdrawal, or add in the case of a deposit) tempAmount from it, which would yield the new balance. Of course, all you're really doing is setting the balance to tempAmount for deposit, and negated tempAmount for withdrawal.
Last edited on
I've not gone through the code in fine detail. However, it looks as though some functions need to modify the contents of the vector userList. In that case the parameter should be passed by reference, not by value. Currently, the parameter is passed by value, meaning a copy is made, and is discarded when the function ends.
Topic archived. No new replies allowed.