-4

First of all, I need to clear some things up: English is not my first language and I may not translate some thing properly. This is just for a school assignment. I don't want anything elaborate. I need to have this finished by the end of January and the fastest I'm done, the better. And seriously that's a hard dead line.

I simply want to be able to read, write and search on a file. Most likely I will need to turn the .txt file into a 2 dimensional string array (I am not sure if that is the correct term).

Basically I am making a program to be put into an ATM. The first .txt file named account.txt is going to have something like:

John 12345
George 11111
Lucas 22222
Mary 33333

The first word being the username the second being the password and they are separated with space. Later I realized I will need a second file named balance.txt to write each account's balance in an integer array that would look like:

65
0
100
150

I need to be able to write into the files in case the person running the program want to create a new account where that person would enter 2 things, a username, a password and then the current balance on the second file.

My code so far is this:

#include <iostream>
using namespace std;
class cust_acc{
private:
    int id;
    double balance;
public:
    cust_acc(){
        cout<<"Account's ID: ";
        cin>>id;
        cout<<"Starting Balance: ";
        cin>>balance;
        cout<<"Account: "<<id<<" created."<<endl<<endl;
        }
    void withdraw(){
        double temp;
        cout<<"Amount to Withdraw: ";
        cin>>temp;
        if(temp<=balance)
            balance-=temp;
            else{
                cout<<"The amount you requested to withdraw is greater than your current balance."<<endl;
                cout<<"Your account was not charged."<<endl;
            }
    }
    void deposit(){
        double temp;
        cout<<"Amount to Deposit: ";
        cin>>temp;
        balance+=temp;
    }
    void inform(){
        cout<<"Your current Balance is: "<<balance<<endl;
    }
};
int main(void){
    int choice;
    cust_acc customer;
    do {
        cout<<"Withdraw: 1"<<endl;
        cout<<"Deposit: 2"<<endl;
        cout<<"Inform: 3"<<endl;
        cout<<"Quit: 4"<<endl;
        cin>>choice;
        switch (choice) {
            case 1:
                customer.withdraw();
                break;
            case 2:
                customer.deposit();
                break;
            case 3:
                customer.inform();
                break;
            case 4:
                break;
            default:
                cout<<"Wrong number."<<endl;
                break;
        }
    }while(choice!=4);
    return(0);
}

This code is straight from my class exactly how I wrote it and I have not made any changes yet. Eventually I will change the id to username and include a password.

I realize I have created a function named cust_acc and I create an object (or whatever it's called) named customer. I don't want to change anything on these things like creating a new object.

I only want to.... I guess open the .txt files on my function's constructor and insert them into probably a string and an integer array respectively and be able to search the files to match the usernames with the passwords in a way like: the user's input goes into a temp variable and then the program searches the first column of the string array (that has inserted the contents of the account.txt file), finds the row it is located on the array (for example account[0][3] (it has actually been a while since we last used arrays so I may be wrong on it's syntax) and then the user writes the password and the program compares it with for example account[1][3].

Also the ability to write on the end of the file with the user giving a username (which will be written on the end of the file while also creating a new row) then the password (which will be written after the username with a space in between them) and finally the starting balance which will be written on the end of balance.txt file (while also creating a new row). The new rows need to be created too so that the different accounts will be distinguishable when they are put into the array variables. Oh and almost forgot the most important: being able to overwrite the file balance.txt

As for my current code I will polish it up, making another menu allowing the user to create a new account.

I tried to search for these thing but everyone was doing their own special thing writing std:: in front of every line and using some random libraries... I could not find a simple valid source.

If it help I though about using something like: (after I put the account.txt into a string 2 dimensional array) read the username, search the first column of the array until it finds a matching username or the search goes to a row where there is nothing written. But my problem is how to search and I realized I do not know how to add a row to any array in case of a new account. Also I could work with 3 one-dimensional arrays (userename.txt, password.txt, balance.txt).






I want to say thank you to everyone that has commented and tried to help me but let me make this a little bit more clear. This is basically what my teacher said:

From the code above (the one we wrote during class) make the necessary so that :

  • You read the username, password and balance from a text file that will serve as a "database"
  • You search your "database" to match the username with its password and balance
  • And then you write on that file because you also have a menu at the beginning in case the user wants to create a new acount He basically asked us to find a way to write, read and search on files.

So the tl;dr version of this as I understand it is I need to have the items listed on this list (after some realization):

  • 3 files, username.txt, password.txt, balance.txt
  • write them on 2 string arrays and 1 integer array variables (respectively) with a single space dividing the each word into different positions in the array
  • Search at least the username array to find the position (I can probably do this myself if I have the rest)
  • Write on the files (even if I have to re-write the entire file, I will find a way to do it if I have the means)
  • And also I may need to be able to change the size of the arrays (since the user can make a new account), unless it's possible to define an array with either undefined size or adjustable or a large size but some positions can be NULL
Community
  • 1
  • 1
  • 5
    You cannot in general modify a text file without completely re-writing it. Text files are not generally appropriate for database implementations. –  Jan 26 '17 at 22:50
  • "I tried to search for these thing but everyone was doing their own special thing writing std:: in front of every line and using some random libraries. "You can just remove that part. You put using namespace std; at the top so you do not have to add it manually. But you are using the same libraries – marsh Jan 26 '17 at 22:50
  • "You put using namespace std; at the top so you do not have to add it manually" You can, [but this is not recommended.](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Jan 26 '17 at 22:52
  • Suggest you focus on getting the implementation correct in memory, then you can add support for writing to (and reading from) the files. – Chad Jan 26 '17 at 22:54
  • long stories are tiresome please in briefly and show the problem – Raindrop7 Jan 26 '17 at 22:54
  • Maybe consider using a simple, single-file, zero-installation/configuration database like `sqlite` as your back-end storage. – Mark Setchell Jan 26 '17 at 22:54
  • `Most likely I will need to turn the .txt file into a 2 dimensional string array ` it is not correct thing but load the text file content into a 2-dimension array – Raindrop7 Jan 26 '17 at 22:56
  • 3
    Recommendation for school use only: Read the files into memory (`std::map` and `std::vector`). Operate in memory. When you make a change, rewrite the entire file. This will keep the job simpler. Also helpful: [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line) answer 1, probably option 2. – user4581301 Jan 26 '17 at 22:59
  • "I need to have this finished by the end of January and the fastest I'm done, the better. And seriously that's a hard dead line." - Irrelevant; we don't care - that's *your* problem. – Jesper Juhl Jan 26 '17 at 23:00
  • Start yourself, then post when you get stuck at each step. You will get better results this way. – marsh Jan 26 '17 at 23:01
  • I also recommend to add an authentication system. Imagine your application is not closed properly on a public computer and someone who shouldn't have access is going to withdraw money. – user3606329 Jan 26 '17 at 23:17
  • I added a tl;dr version. Again thank you all for your contribution but I am not trying to re-write history here by creating the most efficient code. – MainGoldDragon Jan 27 '17 at 07:53

1 Answers1

1

The easiest method for using a text file as a database is to have fixed length fields. This allows for fast reading (reading in blocks) and you can calculate where the record is and seek to it.

Be aware that if you want to modify the text file, you may need to write the entire text file to a new file. However, with a fixed width format, you can modify the records without creating a new file. Inserting, will require writing to a new file.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Hm... if the length of the fields is only going to help with the reading speed.... then I really don't care. This is a simple exercise. I am not writing the code for a company's database. And if I know how to write on a file I could probably find a way to re-write the whole thing. I just almost have a vision as how it will work, I just don't know what are the commands I need to use. – MainGoldDragon Jan 27 '17 at 07:58
  • You can find the methods to position (tellp, tellg) and the methods to `read` and `write` blocks in the `std::iostream` class. – Thomas Matthews Jan 28 '17 at 07:18