Basic banking program with header files

Hi all,

For my programming class I must create a simple banking program with a DB in Array. I've got all that working great. But I'm stuck at one of the points in the task - each function must be in a separate header file.

Can someone help me move one of the functions to a header? :)

Here what I've got so far:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;
 
struct clientData   
{
   int accNum;  
   char Sur[15];    
   char Name[10];   
   float balance;  
   void operator=(clientData * c)
   {
        accNum = c->accNum;
        for(int i=0;i<15;i++)
            Sur[i] = c->Sur[i];
        for(int i=0;i<15;i++)
            Name[i] = c->Name[i];
        balance = c->balance;
   }
};
 
class base
{
    clientData * cl;
    int maxSize;
    int count;
public:
    base()
    {
        maxSize = 100;
        cl = new clientData[maxSize];
        count = 0;
    }
    ~base()
    {
        delete cl;
    }
    void add();
    void print();
    void del();
    void find();
    void setBalance();
    void printDoubt();
    void printCount();
    char PrintMenu();
};

void base::printCount()
{
//Function to print out number of clients
}
void base::add() //To add a new client
{
    cout<<"Input Surname:";
    cin>>cl[count].Sur;
    cout<<"Input Name:";
    cin>>cl[count].Name;
    cout<<"Input Balance:";
    cin>>cl[count].balance;
    cl[count].accNum = count;
    count++;
}
void base::print()
{
//Function to print out all client data
}

void base::del()
{
//Function to delete client
}

void base::setBalance()
{
//Function to change balance
}

void base::find()
{
//Function to find client based on accNum
}

void base::printDebt()
{
//Function to print clients with debt
}

char base::PrintMenu()
{
    char a;
 
        cout<<"1: Add client"<<endl;
        cout<<"2: Deelete client"<<endl;
        cout<<"3: Print all clients"<<endl;
        cout<<"4: Find client"<<endl;
        cout<<"5: Edit balance"<<endl;
        cout<<"6: Print client with debt"<<endl;
        cout<<"7: Print count of clients"<<endl;
        cout<<"q: Quit"<<endl;
        cin>>a;
        return a;
}

int main()
{
    base * b = new base;
    while(true)
    {
        switch(b->PrintMenu())
            {
                case '1':b->add(); break;
                case '2':b->del(); break;
                case '3':b->print(); break;
                case '4':b->find(); break;
                case '5':b->setBalance(); break;
                case '6':b->printDoubt(); break;
                case '7':b->printCount(); break;
                case 'q':exit(1); break;
                default: cout<<"Incorrect letter"; break;
            }
    }
}


Here is what I try to do it:
1: Make add.h and add.cpp files to move the function to add a client
2: Add #include "add.h" to the main file
3: Move this to add.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

void base::add()
{
    cout<<"Input Surname:";
    cin>>cl[count].Sur;
    cout<<"Input Name:";
    cin>>cl[count].Name;
    cout<<"Input Balance:";
    cin>>cl[count].balance;
    cl[count].accNum = count;
    count++;
}

4: Move this to add.h
1
2
3
4
#include <iostream>
using namespace std;

void base::add();


When I run the compiler it says
[Error]'base' has not been declaired
(pointing to add.h file). So the add.h file doesn't see the DB from the main file.

Any tips or ideas?
You have at least have typo.


printDoubt()

or

printDebt()

Also are you sure you mean your teacher wants a separate header file for each function? or a separate header file for each class?
Last edited on
Here is the whole task (translated from Latvian, since the class is in Latvian)

Based on this structure:
struct clientData // client Data
{
int accNum; // account number
char Sur[15]; // surname
char Name[10]; // name
float balance; // balance
};

Create database in form of an array. There must be a main menu with such entries:
* Add entry
* Delete entry
* Print out all entries
* Find an entry (by account number)
* Change account balance (find account by number and input a value to add to the balance)
* Show all clients with debt
* Count entries
* Exit

Additionaly the program should have this:
* Account number must be the index value
* Array size - 100 entries
* Each operation must be in a separate funktion
* Each function must be in a separate header file
* After each operation the user has to be taken back to the main menu.


More or less I have everything except that each function must be in a separate file.
[Error]'base' has not been declaired


You need to include the file where the class base is declared.
That's the main file.
How to include the main file in the header file?
#include "main.cpp" ???
You cant do that. You should probably move the class to a new header, and include that header in main.

move your class base to your add.h
OK I've put clientData and class base in base.cpp and base.h. Also moved add function to add.h and add.cpp.

Here is what I've got now:

main.cpp
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
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include "base.h"
#include "add.h"
using namespace std;

void base::printCount()
{
//Function to print out number of clients
}

void base::print()
{
//Function to print out all client data
}

void base::del()
{
//Function to delete client
}

void base::setBalance()
{
//Function to change balance
}

void base::find()
{
//Function to find client based on accNum
}

void base::printDebt()
{
//Function to print clients with debt
}

char base::PrintMenu()
{
    char a;
 
        cout<<"1: Add client"<<endl;
        cout<<"2: Deelete client"<<endl;
        cout<<"3: Print all clients"<<endl;
        cout<<"4: Find client"<<endl;
        cout<<"5: Edit balance"<<endl;
        cout<<"6: Print client with debt"<<endl;
        cout<<"7: Print count of clients"<<endl;
        cout<<"q: Quit"<<endl;
        cin>>a;
        return a;
}

int main()
{
    base * b = new base;
    while(true)
    {
        switch(b->PrintMenu())
            {
                case '1':b->add(); break;
                case '2':b->del(); break;
                case '3':b->print(); break;
                case '4':b->find(); break;
                case '5':b->setBalance(); break;
                case '6':b->printDoubt(); break;
                case '7':b->printCount(); break;
                case 'q':exit(1); break;
                default: cout<<"Incorrect letter"; break;
            }
    }
}


base.cpp
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
#include "base.h"
struct clientData   
{
   int accNum;  
   char Sur[15];    
   char Name[10];   
   float balance;  
   void operator=(clientData * c)
   {
        accNum = c->accNum;
        for(int i=0;i<15;i++)
            Sur[i] = c->Sur[i];
        for(int i=0;i<15;i++)
            Name[i] = c->Name[i];
        balance = c->balance;
   }
};

class base
{
    clientData * cl;
    int maxSize;
    int count;
public:
    base()
    {
        maxSize = 100;
        cl = new clientData[maxSize];
        count = 0;
    }
    ~base()
    {
        delete cl;
    }
    void add(); //Add client
    void print(); //Show all clients
    void del(); //Delete client
    void find(); //Find cliete
    void setBalance(); //Change balance
    void printDebt(); //Show clients with debt
    void printCount(); //Count clients
    char PrintMenu(); //Show menu
};


base.h
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
#ifndef BASE_H
#define BASE_H
struct clientData   
{
   int accNum;  
   char Sur[15];    
   char Name[10];   
   float balance;  
   void operator=(clientData * c)
   {
        accNum = c->accNum;
        for(int i=0;i<15;i++)
            Sur[i] = c->Sur[i];
        for(int i=0;i<15;i++)
            Name[i] = c->Name[i];
        balance = c->balance;
   }
};

class base
{
    clientData * cl;
    int maxSize;
    int count;
public:
    base()
    {
        maxSize = 100;
        cl = new clientData[maxSize];
        count = 0;
    }
    ~base()
    {
        delete cl;
    }
    void add(); //Add client
    void print(); //Show all clients
    void del(); //Delete client
    void find(); //Find cliete
    void setBalance(); //Change balance
    void printDebt(); //Show clients with debt
    void printCount(); //Count clients
    char PrintMenu(); //Show menu
};
#endif 


add.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "base.h"
using namespace std;

void base::add()
{
    cout<<"Input Surname:";
    cin>>cl[count].Sur;
    cout<<"Input Name:";
    cin>>cl[count].Name;
    cout<<"Input Balance:";
    cin>>cl[count].balance;
    cl[count].accNum = count;
    count++;
};


add.h
1
2
3
4
5
#include <iostream>
#include "base.h"
using namespace std;

void base::add();


Now when compiling I get this error:
[Error] declaration of 'void base::add()' outside of class is not definition [-fpermissive]
(Points to add.h line 5)
Last edited on
Thank you everyone for helping :) Solved it myself.

main.cpp
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
#include <iostream>
#include "base.h"
#include "add.h"
#include "printCount.h"
#include "print.h"
#include "delete.h"
#include "setBalance.h"
#include "find.h"
#include "printDebt.h"
#include "menu.h"
using namespace std;

int main()
{
    base * b = new base;
    while(true)
    {
        switch(b->PrintMenu())
            {
                case '1':b->add(); break;
                case '2':b->del(); break;
                case '3':b->print(); break;
                case '4':b->find(); break;
                case '5':b->setBalance(); break;
                case '6':b->printDebt(); break;
                case '7':b->printCount(); break;
                case 'q':exit(1); break;
                default: cout<<"Incorrect letter"; break;
            }
    }
}


base.h
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
#ifndef BASE_H
#define BASE_H
struct clientData   
{
   int accNum;  
   char Sur[15];    
   char Name[10];   
   float balance;  
   void operator=(clientData * c)
   {
        accNum = c->accNum;
        for(int i=0;i<15;i++)
            Sur[i] = c->Sur[i];
        for(int i=0;i<15;i++)
            Name[i] = c->Name[i];
        balance = c->balance;
   }
};

class base
{
    clientData * cl;
    int maxSize;
    int count;
public:
    base()
    {
        maxSize = 100;
        cl = new clientData[maxSize];
        count = 0;
    }
    ~base()
    {
        delete cl;
    }
    void add(); //Add client
    void print(); //Show all clients
    void del(); //Delete client
    void find(); //Find cliete
    void setBalance(); //Change balance
    void printDebt(); //Show clients with debt
    void printCount(); //Count clients
    char PrintMenu(); //Show menu
};
#endif 


as an example - add.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "base.h"
using namespace std;

void base::add()
{
    cout<<"Input Surname:";
    cin>>cl[count].Sur;
    cout<<"Input Name:";
    cin>>cl[count].Name;
    cout<<"Input Balance:";
    cin>>cl[count].balance;
    cl[count].accNum = count;
    count++;
};


Works like a charm :)
Topic archived. No new replies allowed.