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