Read Access Violation error, c++ on Visual Studio 2015

Hi there, as part of a project at my first year of university, we have been tasked with making a basic banking program that can store customer details, account details etc.

The program needs to be able to handle three customers and up to 5 accounts for each customer, which can be removed and recreated any time.

The error in which I am encountering occurs during the following code, at line 14 when i have "custID[chosenCustInt].account[whatAccount].getBalance()" outputted.

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
 void Withdrawal(Customer custID[], int chosenCustInt) {

	int withdrawAmount = 0;

	int correctAmount = 0;

	int whatAccount = 0;

	
		cout << "\n\nWhat account would you like to withdraw from?\n\n";
		
		cin >> whatAccount;

		cout << "\n\nAccount " << whatAccount << "has \x9C"<< custID[chosenCustInt].account[whatAccount].getBalance() << " available funds.\n\n";


	do {

		cout << "How much would the customer like to withdraw? \n\n1: \x9C" << "10 \n\n2: \x9C" << "25\n\n3: \x9C" << "50\n\n4: \x9C" << "75\n\n5: \x9C" << "100\n\n6: Custom Ammount\n\n";

		cin >> withdrawAmount;

		switch (withdrawAmount) {
		case 1: withdrawAmount = 10; break;
		case 2: withdrawAmount = 25; break;
		case 3: withdrawAmount = 50; break;
		case 4: withdrawAmount = 75; break;
		case 5: withdrawAmount = 100; break;
		case 6: cin >> withdrawAmount; break;
		}

		cout << "Is " << withdrawAmount << " the correct amount?\n\n1: Yes\n\n2: No\n\n";

		cin >> correctAmount;
		
	} while (correctAmount != 1);

	//balance = balance - withdrawAmount;

	
}


I have tried some fixes, such as filling the arrays completely, and originally I was not using getters and setters however this change ahs not fixed it.

One specific thing of note is that I have a function that declares and initialises all the values (names etc) and if i place the code within that function, there is no error which points towards it being an issue with me passing variables between my functions.

thanks in advance, if any more information is needed let me know.

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
#pragma once
#include "Accounts.h"
#include <string>

using namespace std;

class Customer {
public:
	
	Customer();
	~Customer();
	Accounts account[5];
	string name;
	string DOB;
	bool loan;
};


#pragma once
#include <string>
#include "Customer.h"

using namespace std;

class Accounts {
private:

	
	
	
	double balance;
	double overDraft;
	int pinNumber;

public:
	Accounts();
	~Accounts();
	double getBalance() {
		return balance;
	}

	void setBalance(double b) {
		balance = b;
	}
	
	double getOverDraft() {
		return overDraft;
	}
	
	void setOverDraft(double oD) {
		overDraft = oD;

	}

	void setPin(int pin) {
		pinNumber = pin;
	}
};


Last edited on
The error in which I am encountering

What error?


Please show the declaration for your Customer and Account classes.

"Read Access Violation" means you're getting an out of bounds reference. That implies chosenCustInt and/oror whatAccount is invalid.

Line 1: Has chosenCustInt been range checked before entering this function?

Lines 12-14: I see no check that whatAccount is valid (0-4).

Line 13: I would suggest adding a cout of chosenCustInt and whatAccount for debugging purposes.

Why is Withdrawal() a global function and not a member function of your Customer class?

Last edited on
I have added the customer and account class header files to the main topic.

I have not applied any checks for the validity of the variables at the moment, however i am using a set list of numbers i know will work so i can make sure everything else works before i add checks etc.

I added a cout and it is recieving the correct values.

I am very new to coding, and wasn't aware how intense the first year of university would focus on it so I am unsure of many aspects of it, how would i make it part of the customer class?

Thanks.

Update: I *believe* I have successfully made the withdrawal function a member of my customer class, however now within the switch I use to call variables I get the following error:
'Customer::Withdrawal': non-standard syntax; use '&' to create a pointer to member

I will paste update the code for customer.h and customer.cpp to show the function and how it is being defined.

This is the switch case which contains the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void chooseAction(Customer custID[], int chosenCustInt) {
	int custAction;
	cin >> custAction;
	switch (custAction) {
	case 1: custID[chosenCustInt].Withdrawal;		break;
	case 2: Deposit(custID, chosenCustInt);			break;
	case 3: DisplayBalance(custID, chosenCustInt);	break;
	case 4: CalcInterest(custID, chosenCustInt);		break;
	case 5: CreateLoan(custID, chosenCustInt);		break;
	case 6: QuickWithdrawal(custID, chosenCustInt);	break;
	case 7: CreateOverdraft(custID, chosenCustInt);	break;
	case 8: MakeAcc(custID, chosenCustInt);			break;
	}
}


The error occurs on line 5. ( I am yet to change the other functions to be directly within customer class, but i would rather focus on getting at least one working first)

Update 2: Fixed that, forgot i needed to add the parameters. However I have now tested it like that and it still fails to work, simply crashing the program.
Last edited on
Line 5: This is a function call. It needs a pair of ().
 
case 1: custID[chosenCustInt].Withdrawal();		break;


As you move to making your functions members of the Customer class, you will find your coding is easier as you can reference a single customer object and not have to pass the entire array and index to it to every function.

Consider if chooseAction() was passed a single customer instead of the full customer array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void chooseAction (Customer & cust)
{	int custAction;

	cin >> custAction;
	switch (custAction) 
	{
	case 1: cust.Withdrawal();				break;
	case 2: cust.Deposit();				break;
	case 3: cust.DisplayBalance();			break;
	case 4: cust.CalcInterest();			break;
	case 5: cust.CreateLoan();				break;
	case 6: cust.QuickWithdrawal();			break;
	case 7: cust.CreateOverdraft();			break;
	case 8: cust.MakeAcc();				break;
	}
}

One could even argue that chooseAction() should be a member of the Customer class since it is the customer that selects an action. In that case you would not even need to pass a customer reference to chooseAction().

Sorry, I don't have any further insight on your original problem. I would suggest stepping through the code with a debugger.
Topic archived. No new replies allowed.