Search code examples
c++classexc-bad-access

C++ THREAD 1: EXC_BAD_ACCESS(code=1, address =0x8


This is my implementation file for my class bankAccount. So, for some reason, the error message keeps pointing out my function getAccountNumber() where it says "return accountNumber" as the problem. I have other classes within this program, but I don't know why it is specifically pointing out the "return accountNumber" command as the problem. I tried initializing my pointer array with nullptr first for each element of my array in my main function, but that didn't seem to work. Any help would be greatly appreciated.

//
//  impBankAccount.cpp
//  Project 3 Bank Account
//
//  Created by user on 10/20/18.
//  Copyright © 2018 user. All rights reserved.
//

#include <stdio.h>
#include <string>
#include <iomanip>
#include "bankAccount.h"
#include <iostream>


bankAccount :: bankAccount (string n, int acctNumber, double bal){

    name = n;
    accountNumber = acctNumber;
    balance = bal;

}

int bankAccount :: getAccountNumber(){
    return accountNumber;
}

double bankAccount:: getBalance() {
    return balance;
}

string bankAccount:: getName() {
    return name;
}

void bankAccount:: setName(string k){
    name = k;
}

void bankAccount:: withdraw(double k) {
    balance -= k;

    toString = to_string(k);
    toString.erase (toString.find_last_not_of('0') + 3, string::npos );

    transactionsHistory[currentMonth] += "Withdrawal made: $" + toString + "\n";

}

void bankAccount:: deposit(double l){
    balance += l;

    toString = to_string(l);
    toString.erase (toString.find_last_not_of('0') + 3, string::npos );

    transactionsHistory[currentMonth] += "Deposit Made: $" + toString + "\n";
}

void bankAccount:: print() {
    cout << "Name: " << name << endl;
    cout << "Account Balance: " << balance << endl ;
    cout << "Account Number: " << accountNumber << endl;
}

void bankAccount:: setCurrentMonth(int month){
    currentMonth = month;
}


string bankAccount :: getAccountType(){
    return "Bank Account";
}

bankAccount :: ~bankAccount() {


}

This is my code for my main file:

#include <iostream>
#include <iomanip>


#include "bankAccount.h"
#include "savingsAccount.h"
#include "highInterestSavings.h"
#include "noServiceChargeChecking.h"
#include "serviceChargeChecking.h"
#include "highInterestChecking.h"
#include "certificateOfDeposit.h"
#include "checkingAccount.h"

void program(bankAccount *[]);
int getAccount(bankAccount *[], int);


/*
 Account Types:

 Bank Account
 Checking
 Service Charge Checking
 Checking with no service charge
 High Interest Checking
 Certificate of Deposit
 Savings
 High Interest Savings



 */

using namespace std;

int main()
{
    bankAccount *accountsList[6];


    program(accountsList);



    accountsList[0] = new savingsAccount("Bill", 10200, 2500);
    accountsList[1] = new highInterestSavings("Susan", 10210, 2000);
    accountsList[2] = new noServiceChargeChecking("John", 20100,3500);
    accountsList[3] = new serviceChargeChecking("Ravi", 30100, 1800);
    accountsList[4] = new highInterestChecking("Sheila", 20200, 6000);
    accountsList[5] = new certificateOfDeposit("Hamid", 51001, 18000, 0.075, 18);

}
void program(bankAccount *accountList[]){



    int acct = 0;
    int num = 0;




    cout << "What is your account number?";
    cin >> acct;
    num = getAccount(accountList, acct);

    cout << "Welcome to your " << accountList[num]->getAccountType() << " account!";
} // END MAIN

int getAccount(bankAccount *accountList[], int acct){

    int num = 0;

    for(int i =0; i<6; i++){
        if (accountList[i]->getAccountNumber() == acct) {
        num = i;
        break;
        }
    }
    return num;
}

Solution

  • Because accountsList is uninitialized when you call accountsList[i]->getAccountNumber in getAccount.

    Perhaps you meant to populate it first, then call program?