-2

I successfully compiled following codes, but when I tried to run the codes, a "Bus error (core dumped)" occurred every time I finished my first input of "cin >> instruct >> name >> Bal". I searched online about the bus error, but I still couldn't find my error. Please help me with this, thanks a lot !!

  // Bank.h
     1 #ifndef BANK_H
     2 #define BANK_H
     3 using namespace std;
     4 
     5 class BankAccount{
     6     private:
     7     string _name;
     8     double _balance;
     9 
     10     public:
     11     BankAccount(string, double);
     12     string getName();
     13     void setName(string);
     14     double getBalance();
     15     void setBalance(double);
     16     void Withdraw(double);
     17     void Deposite(double);
     18     void interest(int, int);
     19 
     20 };
     21 #endif

 //Bank.cpp
 1 #include<iostream>
 2 #include<string>
 3 #include "Bank.h"
 4 using namespace std;
 5 
 6 BankAccount::BankAccount(string name, double balance):_name(name),
 7 _balance(balance){}
 8 
 9 string BankAccount::getName(){ return _name;}
 10 
 11 double BankAccount::getBalance(){ return _balance;}
 12 
 13 void BankAccount::setName(string name){
 14     _name = name;
 15     return;
 16 }
 17 
 18 void BankAccount::setBalance(double balance){
 19     _balance = balance;
 20     return;
 21 }
 22 
 23 void BankAccount::Withdraw(double balance)
 24 {
 25 
 26     _balance = _balance - balance;
 27     return;
 28 }
 29 
 30 void BankAccount::Deposite(double balance)
 31 {
 32 
 33     _balance = _balance + balance;
 34     return;
 35 }
 36 
 37 void BankAccount::interest(int interestRate, int M)
 38 {
 39     double interest;
 40 
 41     interest = _balance*(interestRate/1200*1.0)*M;
 42     _balance  = _balance + interest;
 43 
 44     return;
 45 }


  //BankMain.cpp
  1 #include<iostream>
  2 #include<string>
  3 #include "Bank.h"
  4 using namespace std;
  5 
  6 int main()
  7 {
  8     int x, p, check=1, i=0, j;
  9     double Bal;
 10     BankAccount* Account[100];
 11     string name;
 12     string instruct;
 13 
 14     cin >> x >> p;
 15 
 16     while(check)
 17     {
 18         cin >> instruct >> name >> Bal;
 19 
 20         if(instruct == "Create")
 21         {
 22             Account[i]->setName(name);
 23             Account[i]->setBalance(Bal);
 24             Account[i]->interest(x, p);
 25             i++;
 26         }
 27         else
 28         {
 29             if(instruct == "Withdraw")
 30             {
 31                 for(j=0; j<i;j++)
 32                 {
 33                     if(Account[j]->getName() == name)
 34                         break;
 35                         }
 36                         Account[j]->Withdraw(Bal);
 37 
 38             }
 39 
 40             if(instruct == "Deposite")
 41             {
 42                 for(j=0; j<i; j++)
 43                 {
 44                     if(Account[j]->getName() == name)
 45                         break;
 46                 }
 47                 Account[j]->Deposite(Bal);
 48             }
 49         }
 50 
 51         if(instruct == "0")
 52             check = 0;
 53     }
 54 
 55     cout << i;
 56     for(j=0; j<i; j++)
 57     {
 58        cout << Account[j]->getName() << " " << Account[j]->getBalance();
 59         cout << endl;
 60     }
 61 
 62     return 0;
 63 }
SQL
  • 21
  • 1
  • 4
  • 1
    Best you start your debugger and step through your code line by line to find the source of the error. – πάντα ῥεῖ Sep 13 '15 at 11:33
  • 1
    So you have read [What is a bus error?](http://stackoverflow.com/questions/212466/what-is-a-bus-error) And you checked, that you aren't, for instance "using an uninitialized hence bogus pointer." ? – Leiaz Sep 13 '15 at 11:40

1 Answers1

1

You have many pitfalls in your code. The first one is that you defined an array of pointers BankAccount* Account[100]; and you access it as they are initialized... Why? This array contains junk and if you need to create a new account use new BankAccount(name, balance) first and assign it to the appropriate index in this array.

Then all your internal loops which scan for a specific name assume that this name is found and access Account[j]->... but what if j==i and this name was not found?

These are the main ones that I saw. Of course there are more but they should not cause "core dump". Like passing string by value or dividing integer by 1200 (if that integer is smaller than 1200 you will get 0).

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • The problem is fixed ! and it was due to the array of pointer. As I tried to create a new account by using new BankAccount(name, balance), it worked. Thanks a lot ! – SQL Sep 13 '15 at 12:10