In-Memory Database using Array of Structs

I wrote most of a simple bank software program for an intro class in C++ using Visual C++ 2010, however apparently it needs to be an in-memory database (didn't notice that...) which I have never done before and I'm unsure how to go about it.

The program needs to have three interfaces.

The first is a customer interface that lets you check balance of an account, transfer funds between the various accounts (I only made accounts for checking, savings, and a credit line), and make deposits and withdrawals from specific accounts.

The second is a bank teller interface, which has access to the customer interface (it can modify the account data the same way), which isn't exactly hard to do. Anyway, it also needs to be able to add and delete users, and use a sequential search based on a customer's name.
The accounts are made with a struct that had the name, PIN, and amounts in each account type that was just saved to a file in order (the first two lines were the name, the third the PIN, and the last three the accounts, so when someone dealt with transfers or something I just targeted the line it was on and pulled out that info to put into a variable for modification, then overwrote the line with the new number). The search function took input from two strings where the name was put in at, and then compared them to the first two lines of a text file until it found the right file, then displayed it and from there the user could modify or delete the file.

The last interface is a management UI, which has the same powers as the teller UI, and then it would have options to list number of customers in the database, the cumulative amount of money in the bank, and then lists of deposits and withdrawals in a day and a log of all transactions.
To do this bit I just had different text documents that were searched through.
For the transaction details I had one text document, to which a new line was added every time there was a change (the line would be like "01302011 SAVE 100.00 W 20.00 80.00 JOHN DOE 12:00:00", where the first number is the date, SAVE indicates the saving's account, 100.00 is the balance before any action is taken, W mean's withdrawal, 20.00 is the amount withdrawn, 80.00 is the amount left, then the name of the customer and the date).
For the amount of customers and the amount of money in the bank, I had two other text files. One stored the amount of money in the bank. Anytime there was a change, it would put that number into a float that was modified and then overwrote the first line. The other had a list of the names of each customer, each customer stored on one line. The first line was the number of customers.

And to be honest I have no clue how I would do this as an in-memory DB. I've never even tried to make one before.

I'm guessing that I need to do something like create an array of structs.

So off the top of my head I would do something like:

1
2
3
4
5
6
7
8
int arraysize;

struct AccountData
{
     char[20] firstname;
     char[20] lastname;
     float checking = 0, savings = 0, creditcard = 0;
} account[arraysize];


to set up the basic struct array?

I'm not entirely sure how I would set up a dynamic array though, so that every time you add an account it dynamically adds one to the arraysize. Especially given that adding an account would be it's own function, so when I go back how would I test to see what index I'm on (if I were to be adding a fifth account, it would need to go to index 4 for the array so would I just use the increment operator on arraysize? Is there a way to be sure it does it right every time, as I need to initialize it to 0 at the start so that it assign the proper index to the structs?)

And how should I organize it? I can't use any global variables, so with everything localized how would I get the information saved in that struct if I were in a different function, such the search function instead of addcustomer? I know I could use pointers, but how would I carry the address data across functions, or would I not need to for some reason?

If anyone has any tutorials regarding this or can explain parts of it to me, it would be greatly appreciated.
Let's see...
Nowhere do they say you have to use an array. You could just as easily use a vector (EDIT: it's easier, actually). Do you know how to use vectors? If not, here are a few code snippets to help out:
1
2
3
4
5
6
#include <vector> //You need to have this in your C++ files to use vectors.
std::vector<AccountData> account; //This creates an empty vector of AccountDatas.
account.push_back(); //Pass as an argument to push_back() an AccountData object.
//The above will append it to your vector, lengthening it.
account.at(position); //Works like the [] operator, except it's safer.
account.size(); //Should you need this, this returns the size of your vector. 


Also, do you know how to pass by reference in functions? This is a neat feature of C++ that you should know, though I think the tutorial could explain it better than I can.
http://cplusplus.com/doc/tutorial/functions2/

Do these help?

-Albatross
Last edited on
The documents say "The database is a simple in-memory database implemented using arrays of structs." I believe the intention is for us to expand on it with more advanced structures and classes as the class progresses so that we're working continuously with familiar code. I kind of summarized the assignment documents because it's several pages long.

And yes I do know what those are, however I forgot about them.

I've kind of figured some things out and I'm trying to rewrite the system to add records so that I can come back with a hopefully more detailed question than "Tell me how to do this". And thank you for the tutorial, that will probably get me a lot further.
Topic archived. No new replies allowed.