0

me and my partner are having a tough time trying to find out how to fix the error on our code. Were engineering undergrads and programming isn't exactly our forte so please take it easy on us.

Im sorry if I'm sending the whole code, but Im quite desperate since we need this project to pass the course. Thanks. Here is my code:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#include <iomanip>
using namespace std;

struct baccount {
    int pin;
    char accholder[50];
    int deposit;
    void create_account();
    void display_account() const;
    void modify();
    void depmoney(int);
    void drawmoney(int);
    void report() const;
    int retpin() const;
    int retdeposit() const;
};

void create_account()
{
    struct baccount qeqe;
    cout << "\nEnter Pin Number:";
    cin >> qeqe.pin;
    cout << "\n\nEnter your name : ";
    cin.ignore();
    cin.getline(qeqe.accholder, 50);
    do {
        cout << "\nEnter the initial amount to be stored in the account(Minimum P2000) : ";
        cin >> qeqe.deposit;
    } while (qeqe.deposit < 2000);

    cout << "\n\n\nAccount Created..";
};

void display_account()
{
    struct baccount qeqe;
    cout << "\nPin Number: " << qeqe.pin;
    cout << "\nAccount Holder Name : ";
    cout << qeqe.accholder;
    cout << "\nCurrent Balance : " << qeqe.deposit;
}

void depmoney(int x)
{
    struct baccount qeqe;
    qeqe.deposit += x;
}

void modify()
{
    struct baccount qeqe;
    cout << "\nPin Number : " << qeqe.pin;
    cout << "\nModify Account Holder Name : ";
    cin.ignore();
    cin.getline(qeqe.accholder, 50);
    cout << "\nModify Balance amount : ";
    cin >> qeqe.deposit;
}

void drawmoney(int x)
{
    struct baccount qeqe;
    qeqe.deposit -= x;
}

void report()
{
    struct baccount qeqe;
    cout << qeqe.pin << setw(15) << " " << qeqe.accholder << setw(15) << " " << setw(8) << qeqe.deposit << endl;
}

int retpin()
{
    struct baccount qeqe;
    return qeqe.pin;
}

int retdeposit()
{
    struct baccount qeqe;
    return qeqe.deposit;
}

void write_account();
void display_sp(int);
void modify_account(int);
void delete_account(int);
void display_all();
void deposit_withdraw(int, int);

int main()
{
    char choice;
    int pnum;
    do {
        system("cls");
        cout << "Welcome to Cutaran-Eleria Bank" << endl;
        cout << "[1] Create new account" << endl;
        cout << "[2] Deposit" << endl;
        cout << "[3] Withraw" << endl;
        cout << "[4] Show current balance" << endl;
        cout << "[5] Check list of accounts" << endl;
        cout << "[6] Remove an account" << endl;
        cout << "[7] Edit an account" << endl;
        cout << "[8] Exit Cutaran-Eleria Bank" << endl;
        cout << "Please enter the command you want to do: " << endl;
        cin >> choice;
        system("cls");
        switch (choice) {
        case '1':
            write_account();
            break;
        case '2':
            cout << "\n\n\tEnter Pin Number: ";
            cin >> pnum;
            deposit_withdraw(pnum, 1);
            break;
        case '3':
            cout << "\n\n\tEnter The account No. : ";
            cin >> pnum;
            deposit_withdraw(pnum, 2);
            break;
        case '4':
            cout << "\n\n\tEnter The account No. : ";
            cin >> pnum;
            display_sp(pnum);
            break;
        case '5':
            display_all();
            break;
        case '6':
            cout << "\n\n\tEnter Pin Number : ";
            cin >> pnum;
            delete_account(pnum);
            break;
        case '7':
            cout << "\n\n\tEnter Pin Number. : ";
            cin >> pnum;
            modify_account(pnum);
            break;
        case '8':
            cout << "\n\nThank you for using Cutaran-Eleria Bank";
            break;
        default:
            cout << "\a";
        }
        cin.ignore();
        cin.get();
    } while (choice != '8');
    return 0;
}

void write_account()
{
    struct baccount qeqe;
    ofstream newrecord;
    newrecord.open("accounts.dat", ios::binary | ios::app);
    qeqe.create_account();
    newrecord.write(reinterpret_cast<char*>(&qeqe), sizeof(baccount));
    newrecord.close();
    cout << "Your account is now saved into the system.";
}

void display_sp(int n)
{
    baccount bankacc;
    bool flag = false;
    ifstream oldrecords;
    oldrecords.open("accounts.dat", ios::binary);
    if (!oldrecords) {
        cout << "The account is not in the system";
        return;
    }
    cout << "\nCurrent Balance\n";

    while (oldrecords.read(reinterpret_cast<char*>(&bankacc), sizeof(baccount))) {
        if (bankacc.retpin() == n) {
            bankacc.display_account();
            flag = true;
        }
    }
    oldrecords.close();
}

void modify_account(int n)
{
    bool found = false;
    baccount acc;
    fstream File;
    File.open("accounts.dat", ios::binary | ios::in | ios::out);
    if (!File) {
        cout << "Unable to open account.";
        return;
    }
    while (!File.eof() && found == false) {
        File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
        if (acc.retpin() == n) {
            acc.display_account();
            cout << "\n\nEnter The New Details of account" << endl;
            acc.modify();
            int pos = (-1) * static_cast<int>(sizeof(baccount));
            File.seekp(pos, ios::cur);
            File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
            cout << "\n\n\t Record Updated";
            found = true;
        }
    }
    File.close();
    if (found == false)
        cout << "\n\n Record Not Found ";
}

void delete_account(int n)
{
    baccount acc;
    ifstream oldrecords;
    ofstream newrecord;
    oldrecords.open("accounts.dat", ios::binary);
    if (!oldrecords) {
        cout << "File could not be opened!!! Press any Key...";
        return;
    }
    newrecord.open("Temp.dat", ios::binary);
    oldrecords.seekg(0, ios::beg);
    while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
        if (acc.retpin() != n) {
            newrecord.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
        }
    }
    oldrecords.close();
    newrecord.close();
    remove("accounts.dat");
    rename("Temp.dat", "accounts.dat");
    cout << "\n\nRecord Deleted ..";
}

void display_all()
{
    baccount acc;
    ifstream oldrecords;
    oldrecords.open("accounts.dat", ios::binary);
    if (!oldrecords) {
        cout << "Unable to show accounts";
        return;
    }
    cout << "\n\n\t\tList of Accounts\n\n";

    cout << "Pin Number      Name        CurrentBalance\n";

    while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
        acc.report();
    }
    oldrecords.close();
}

void deposit_withdraw(int n, int option)
{
    int amt;
    bool found = false;
    baccount acc;
    fstream File;
    File.open("accounts.dat", ios::binary | ios::in | ios::out);
    if (!File) {
        cout << "Unable to access the account.";
        return;
    }
    while (!File.eof() && found == false) {
        File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
        if (acc.retpin() == n) {
            acc.display_account();
            if (option == 1) {
                cout << "\n\nEnter The amount to be deposited";
                cin >> amt;
                acc.depmoney(amt);
            }
            if (option == 2) {
                cout << "\n\nEnter The amount to be withdrawn";
                cin >> amt;
                int bal = acc.retdeposit() - amt;
                if (amt > acc.retdeposit())
                    cout << "Insufficient balance";
                else
                    acc.drawmoney(amt);
            }
            int pos = (-1) * static_cast<int>(sizeof(acc));
            File.seekp(pos, ios::cur);
            File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
            cout << "\n\nRecord Updated";
            found = true;
        }
    }
    File.close();
    if (found == false)
        cout << "\n\n Record Not Found ";
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • What are the *exact* error messages you're getting? (I.e. which names are undefined?) – NPE Aug 13 '17 at 14:42
  • 1
    Wait, do you understand that `retpin` & `retdeposit` will return garbage values because you return data from an uninitialized `struct baccount qeqe` that's created every time you call these functions? – ForceBru Aug 13 '17 at 14:45
  • Also, `create_account` is just as useless as it is not tied to the struct in any way. You should call it `baccount::create_account`. The struct acts in the same way as a class. – ForceBru Aug 13 '17 at 14:46
  • 1
    What are the *exact* error messages you're getting? (I.e. which names are undefined?) – NPE Aug 13 '17 at 14:48
  • `while (!File.eof() && found == false) {` You may want to reconsider this code: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Aug 13 '17 at 14:57

2 Answers2

2

"undefined reference to ..." Is a linker error, not a compiler error. It means that you forgot to link something (library, object file) that defines whatever type the error is complaining about. Add the missing object file or library to your linker commandline and you should be good.

Do note that, depending on toolchain, the order in which you link things may be significant. Always order your link so that objects depending on types are mentioned before the objects that implement them.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

You are getting undefined reference to becouse you havent function definition (in this case).

It means that linker cannot find function's (which you are trying to call) definition.

For example, what you have to do, is:

Outside of struct

void baccount::display_account() const
{
    std::cout << "Now you can call me!";
}

Or right in the struct

struct baccount
{
    int pin;
    void display_account() const
    {
        std::cout << "Now you can call me!";
    }
    .
    .
    .
};
kocica
  • 6,412
  • 2
  • 14
  • 35