-3

I'm working on a program that insert, delete accounts from a Bank.

Here is my .hpp code :

#ifndef DEF_BANK
#define DEF_BANK

#include <iostream>

using namespace std;

class Bank
{

private:
    class _Account
    {
    public:
        _Account(string, float);
        string getClient();
        float getBalance();
        _Account *getNext();

        void setClient(string);
        void setBalance(float);
        void setNext(Bank::_Account *);

    private:
        string _client; //nom client
        float _balance; // stocke balance du compte
        _Account *_next; // next account
    };

    _Account *_head;
public:
    Bank();
    Bank(string name, float balance);
    _Account *rechercheClient(string);
    float withdraw(string, float);
    float deposit(string, float);
    void createAccount(string, float);
    void insert(string, float);
    void remove(string name);
    float deleteAccount(string);
    void mergeAccounts(string, string);
    void displayAccounts();

};

#endif

And here is my .cpp insert function:

void Bank::insert(string name, float balance)
{
    _Account *temp(_head);
    //_Account *n = new _Account(name, balance); 
    bool flag(true);

    while(temp)
    {
        if (temp->getClient() == name)
        {
            /* code */
            cout << "DENIED OPERATION! --> "<< name <<"’s account already exists." << endl;
            flag = false;
        }

        temp = temp->getNext();
    }

    if (flag)
    {
        /* code */
        temp->setNext(new _Account(name, balance));
    }

}

Why when I try this in main.cpp:

int main()
{
    Bank account_1;
    account_1.insert("Hamza", 1000.0);
}

I get a segmentation fault :11 ? because I don't see my fault in the code.

IdelHamza
  • 15
  • 5
  • 2
    When you use your debugger, what is the value of `Account *_head` ? – Soren May 11 '16 at 18:30
  • Question: Why does (half of) an intrusive linked list implementation belong in a bank Account class? – Tim Seguine May 11 '16 at 18:34
  • The teacher told us to make It like that :) – IdelHamza May 11 '16 at 18:46
  • https://en.wikipedia.org/wiki/Single_responsibility_principle Also unless you need lock-free data structures or are doing a homework assignment there is essentially no good reason to implement a linked-list in C++ by hand. – Tim Seguine May 11 '16 at 18:56
  • And do you have a solution concerning the insert method please ? – IdelHamza May 11 '16 at 19:00
  • `using namespace std;` is best used with caution and a dangerous thing to put in a header. More here: [Why is “using namespace std” in C++ considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – user4581301 May 11 '16 at 19:33
  • When writing a linked list, you need to visualize how the items are linked. One of the best ways to do this is to draw each operation out on a piece of paper step by step. – user4581301 May 11 '16 at 19:34

2 Answers2

4
bool flag(true);

while(temp)
{
    if (temp->getClient() == name)
    {
        /* code */
        cout << "DENIED OPERATION! --> "<< name <<"’s account already exists." << endl;
        flag = false;
    }

    temp = temp->getNext();
}

if (flag)
{
    /* code */
    temp->setNext(new _Account(name, balance));
}

This doesn't make sense. Control leaves the while loop once temp points to nullptr. Then you try to dereference that pointer with temp->setNext(new _Account(name, balance));. That's Undefined Behaviour.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

As the other answer pointed out, your loop is wrong. If you change the last line from this:

temp = temp->getNext();

to this:

if (temp->getNext()) {
    temp = temp->getNext();
} else {
    break;
}

Then your loop should stop at the last element in the list instead of the (non existent) element after the last element in the list.

The real problem however is that your teacher thinks this is a good way to teach a beginner C++.

Tim Seguine
  • 2,887
  • 25
  • 38
  • I try this out, but still the same problem: segmentation fault:11. – IdelHamza May 11 '16 at 21:56
  • Here is my actual code `void Bank::insert(string name, float balance) { _Account *temp(_head); //_Account *n = new _Account(name, balance); bool flag(true); while(temp) { if (temp->getClient() == name) { /* code */ cout << "DENIED OPERATION! --> "<< name <<"’s account already exists." << endl; flag = false; } if (temp->getNext()) { temp = temp->getNext(); } else { break; } } if (flag) { /* code */ temp->setNext(new _Account(name, balance)); } }` – IdelHamza May 11 '16 at 21:57