calling a member function, unintended returns

I'm working on a project that has several banking menu options and calls all the accountID, passcode, balance, etc. from an object defined in the header.
Here's the thing I can call the accountID and passcode and accurately check them for validity but when I try to call GetBalance it runs but returns and updates a crazy long number that I definitely never put in the account Balance.
I need help calling the accurate account balance.
I think that even though I'm declaring an account variable that it isn't the one found with the code below from accountID and passcode.
I'll be checking in all night that s just partial code below if you want the whole thing in three parts : bankingSystem.h, bankingSystemDriver.cpp, and bankingSystem.cpp. just let me know. The header and driver are small and working fine.
Thanks!

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
 void BankingSystem::Deposit()
{
	Account     account;
	size_t   index;
    unsigned accountID;
    unsigned passcode;
	double deposit;
	double balance;


	std::cout << "\nEnter the account ID: ";
	std::cin >> accountID;
	if (FindAccount(accountID) == current_num_accounts_)
	{
		std::cout << "\nAccount not found!" << std::endl;
	}
		else
		{
			index = FindAccount(accountID);
			std::cout << "\nEnter the passcode: ";
			std::cin >> passcode;
			if (MatchPasscode(passcode, index) == 1)
			{
				std::cout << "\nHow much would you like to deposit? ";
				std::cin >> deposit;
				if (deposit > 0)
				{
					balance = account.GetBalance();
					balance = balance + deposit;
					account.SetBalance(balance);
					std::cout << std::setprecision(2) << std::fixed << "\nSuccess! The new balance is " << balance << "!" << std::endl;
				}
				else
We'll need more code. I'm guessing that your balance variable is uninitialized.
Yes, the other codes would be useful. You could initialize the value in the default constructor. You declare it and define it just like any other function. It helps to prevent uninitialized variables.

1
2
3
4
5
6
7
BankingSystem(){

balance = 0;
deposit = 0;
//... and so on. You define every variable in the class here. 
//If your variables contain another class, you have to define those too.
}
Last edited on
So this is all of the banking system.cpp. There's also a bankingsystemdriver.cpp but that's mostly just a menu and switch between menu items and there's also a bankingsystem.h header that mostly defines an Account class and a banking system class but the account is just getters and setters for accountid, passcode, first and last names, and balance and the banking system class implementation is all mostly fleshed out right here.
I feel like calling account.Getbalance is right but that it's calling them goofily.
I'll try initializing balance and deposit to zero though just in case they are having residual values.
Thanks!


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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
*/

#ifndef _BANKINGSYSTEM_CPP
#define _BANKINGSYSTEM_CPP

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "bankingSystem.h"

void Account::SetAccountID(unsigned accountID)
{

    accountID_ = accountID;

}

unsigned Account::GetAccountID()
{

    return accountID_;

}

//*** TO DO:  Add the rest of your Account function implementations here. *NS* I'm doing void Account instead of Void
//Passcode because I know that all of these are supposed to go under one element of an array. Not sure of the necessity of that?
//Same for the following Account function implementations.
void Account::SetPasscode(unsigned int passcode)
{
	passcode_ = passcode;
}

unsigned Account::GetPasscode()
{
	return passcode_;
}

void Account::SetFirstName(const std::string& firstName)
{
	firstName_ = firstName;
}

std::string Account::GetFirstName()
{
	return firstName_;
}

void Account::SetLastName(const std::string& lastName)
{
	lastName_ = lastName;
}

std::string Account::GetLastName()
{
	return lastName_;
}

void Account::SetBalance(double balance)
{
	balance_= balance;
}

double Account::GetBalance()
{
	return balance_;
}

//Default constructor just initializes
//current number of accounts to 0;

BankingSystem::BankingSystem() : current_num_accounts_(0)
{}

//Find an account by accountID.
//Return the array index if found, current_num_accounts_ otherwise.

size_t BankingSystem::FindAccount(size_t accountID)
{

    for (size_t i = 0; i < current_num_accounts_; ++i)
        if (accounts_[i].GetAccountID() == accountID)
            return i;

    return current_num_accounts_;

}

//Match the passcode at a given index with the given passcode.
//Return true for a match, false otherwise.

bool BankingSystem::MatchPasscode(unsigned passcode, size_t index)
{

    return accounts_[index].GetPasscode() == passcode;

}

//Add an account.

void BankingSystem::CreateAccount()
{

    Account     account;
    unsigned    accountID;
    unsigned    passcode;
    std::string firstName;
    std::string lastName;
    double      balance;

    //Get the account ID and make sure it doesn't already exist.

    std::cout << "\nEnter the account ID:  ";
    std::cin >> accountID;

    if (FindAccount(accountID) != current_num_accounts_)
	{

        std::cout << "\nAccount already exists!\n";

        return;
	}
  
	
	account.SetAccountID(accountID);
	std::cout << "\nEnter the passcode: ";
	std::cin >> passcode;
	account.SetPasscode(passcode);
	std::cout << "\nEnter the first name: ";
	std::cin >> firstName;
	account.SetFirstName(firstName);
	std::cout << "\nEnter the last name: ";
	std::cin >> lastName;
	account.SetLastName(lastName);
	std::cout << "\nEnter the starting balance: ";
	std::cin >> balance;
	account.SetBalance(balance);

	//Set this info into your Account object.
    //Add the account to your array and increment the count of accounts.

    accounts_[current_num_accounts_] = account;
    ++current_num_accounts_;

    std::cout << "\nAccount added successfully!\n";

}

//Delete an account.
void BankingSystem::DeleteAccount()
{

    size_t   index;
    unsigned accountID;
    unsigned passcode;

    //*** TO DO:  Get the account ID and verify it exists.
	std::cout << "\nEnter the account ID: ";
	std::cin >> accountID;
	if (FindAccount(accountID) == current_num_accounts_)
	{
		std::cout << "\nAccount not found!" << std::endl;
	}
	else
	{
	index = FindAccount(accountID);
	std::cout << "\nEnter the passcode: ";
	std::cin >> passcode;
	if (MatchPasscode(passcode, index) == 1)
	{
	for (size_t i = index; i < current_num_accounts_ - 1; ++i)
        accounts_[i] = accounts_[i + 1];
	--current_num_accounts_;
	std::cout << "\nAccount erased!\n";
	}
	else
	{
		std::cout << "\nInvalid passcode!" << std::endl;
	}
	}
}

//*** TO DO:  Add your remaining BankingSystem function implementations here.

void BankingSystem::PrintAccounts()

{
	

	std::cout << "nope";
}

void BankingSystem::Deposit()
{
	Account     account;
	size_t   index;
    unsigned accountID;
    unsigned passcode;
	double deposit;
	double balance;


	std::cout << "\nEnter the account ID: ";
	std::cin >> accountID;
	if (FindAccount(accountID) == current_num_accounts_)
	{
		std::cout << "\nAccount not found!" << std::endl;
	}
		else
		{
			index = FindAccount(accountID);
			std::cout << "\nEnter the passcode: ";
			std::cin >> passcode;
			if (MatchPasscode(passcode, index) == 1)
			{
				std::cout << "\nHow much would you like to deposit? ";
				std::cin >> deposit;
				if (deposit > 0)
				{
					balance = account.GetBalance();
					balance = balance + deposit;
					account.SetBalance(balance);
					std::cout << std::setprecision(2) << std::fixed << "\nSuccess! The new balance is " << balance << "!" << std::endl;
				}
				else
				{
					std::cout << "\nYou must deposit at least a penny!" << std::endl;
				}
			}
			else
			{
				std::cout << "\nInvalid passcode!" << std::endl;
			}
	}

}

void BankingSystem::Withdraw()

{
	Account     account;
	size_t   index;
    unsigned accountID;
    unsigned passcode;
	double withdraw;
	double balance;


	std::cout << "\nEnter the account ID: ";
	std::cin >> accountID;
	if (FindAccount(accountID) == current_num_accounts_)
	{
		std::cout << "\nAccount not found!" << std::endl;
	}
		else
		{
			index = FindAccount(accountID);
			std::cout << "\nEnter the passcode: ";
			std::cin >> passcode;
			if (MatchPasscode(passcode, index) == 1)
			{
				std::cout << "\nHow much would you like to withdraw? ";
				std::cin >> withdraw;
					balance = account.GetBalance();
					balance = balance - withdraw;
				if (balance < 0)
				{
					std::cout << "\nSorry!  You don't have that much money to withdraw!" << std::endl;
				}
				else
				{
					account.SetBalance(balance);
					std::cout << std::setprecision(2) << std::fixed << "\nSuccess! The new balance is " << balance << "!" << std::endl;
				}
			}
			else
			{
				std::cout << "\nInvalid passcode!" << std::endl;
			}
	}

}

#endif 
Last edited on
So I just tried initializing deposit and balance to zero at the beginning of the deposit implementation and I'm still getting the crazy long negative number.
> I think that even though I'm declaring an account variable
> that it isn't the one found with the code below from accountID and passcode.
Exactly.
You never set `account' to anything meaningful.
I realize that but I'm also unsure of exactly how to set it correctly to correspond with the other info. I've tried using FindAccount to correspond with account like it does for accountID but so far it's a no go.
You have a couple of choices.

1) you can set the local account instance from accounts_[index], but then you have to store it back the modified instance.
1
2
3
4
5
6
7
8
9
10
11
  Account account; 

  index = FindAccount (accountID);
  if (index == current_num_accounts_)
  { /* not found */ 
  }
  else
  {  account = accounts_[index];  // Copy the account instance
      /* modifiy local account */ 
     accounts_[index] = account;  // Copy it back
  }


2) Or simply use a pointer
1
2
3
4
5
6
7
8
9
10
  Account *   acct; 

  acct = FindAccount (accountID);  // Returns a pointer or NULL if not found
  if (! acct)
  { /* not found */ 
  }
  else
  {  /* modifiy account via ptr */ 
      acct->SetBalance (newbalance);
  }



BTW, you don't need include guards around a .cpp file. A .cpp file should never be included.



Last edited on
[
Last edited on
Since the OP has included member functions for Account (Account::SetAccountID), we can infer it is a class or a struct.
Topic archived. No new replies allowed.