Hi guys, have a school project that i am working on but i am just running into this one problem. Well its not really a problem its more of a me just starting the c++ course and i just wanted to do something extra to get some more marks. We had to make a bank ATM program which would be liek the real thing, i decided that instead of defining the balance, i would have it read from a txt file and from there i could read/write to it. We haven tlearned this in class but i thought i would give it a shot.

I just wanted to know how i would add/subtract from a txt file? Say the user wants to withdraw 400$ out of the 500$ that the txt contains. How would i do this?(i know formating is off, i pasted it into google docs from school to open it at home)

ya so i would appreciate it if someone could tell me how i would go about adding/withdrawing from my txt file.

here is some of my code.

//BANK ATM PROGRAM.

 

#include <fstream>

#include <iostream>

using namespace std;

int main ()

{

double pass = 0;

double choice;

int acc1;

int acc2;

int amount;

int deposit;

 

cout<<"Please enter you PIN code"<<endl;

cin>>pass;

if (pass == 5555)

cout<<"PIN VALIDATED...."<<endl;

do

{

if (pass != 5555)

{

cout<<"Please enter you PIN Again code"<<endl;

cin>>pass;

}

 

}while( pass != 5555);

system("cls");

 

while(pass == 5555)

{

cout<<"__________________________________________________________________________"<<endl;

cout<<" ATM MACHINE "<<endl;

cout<<"__________________________________________________________________________"<<endl;

 

cout<<"Please pick one of the below options..."<<endl;

cout<<"amountDRAW FUNDS"<<endl;

cout<<"DEPOSIT"<<endl;

cout<<"TRANSFER"<<endl;

cout<<"CHECK BALANCE"<<endl;

cout<<"CLOSE"<<endl;

cin>>choice;

if (choice == 1)

{

{

cout<<" PLEASE PICK AN ACCOUNT"<<endl;

cout<<"1) ACCOUNT ONE: "<<endl;

cout<<"2) ACCOUNT TWO: "<<endl;

cin>>acc1;

}

}

if (choice == 1 && acc1 == 1)

{

{

system("cls");

cout<<"____________________________"<<endl;

cout<<" ACCOUNT ONE" <<endl;

cout<<"____________________________"<<endl;

char bank [200];

fstream file_op("G:\\bank.dat",ios::in);

while(file_op >> bank)

cout<<"BANK BALANCE FOR ACCOUNT "<<bank<<"$"<<endl;

file_op.close();

if (acc1 == 1)

cout<<"HOW MUCH WOULD YOU LIKE TO WITHDRAW"<<endl;

cin>>amount;

}

}

 

if (choice == 1 && acc1 == 2)

{

{

system("cls");

cout<<"____________________________"<<endl;

cout<<" ACCOUNT TWO" <<endl;

cout<<"____________________________"<<endl;

char bank2 [200];

fstream file_op("G:\\bank2.dat",ios::in);

while(file_op >> bank2)

cout<<"BANK BALANCE FOR ACCOUNT "<<bank2<<"$"<<endl;

system("pause");

system("cls");

file_op.close();

if (acc1 == 2)

cout<<"HOW MUCH WOULD YOU LIKE TO WITHDRAW"<<endl;

cin>>amount;

}

}

if (choice == 2)

{

char bank1 [200];

fstream file_op("G:\\bank1.dat",ios::in);

while(file_op >> bank1)

cout<<"ACCOUNT ONE"<<bank1<<endl;

cout<<"HOW MUCH TO DEPOSIT"<<endl;

 

cin>>deposit;

file_op.close();

 

 

}

 

 

 

 

system("pause");

return 0;

 

}

}

With text files I don't think there is much option but to read in the whole file, and write out the whole file again, but this time with the new value in the place of the old value. I think you'd have to use an actual database to be able to simply change that one value.

Dave

oh really?well i dont know how to do that as we are only 5 weeks/6months into the course so i think it would be better for me to predefine the amount of $ in the account? rather than reading and writing to the txt file for now?

You can edit file contents without reading the whole file directly into a program, under some circumstances. To my knowledge there are several situations where this is possible.

Under one scenario you read in just the data that needs to be edited, make the changes and write it back. You need to make sure the data you replace takes up the same amount of information as the data you are replacing it with. If that's not the case, then the data in the file will be corrupted.

Under another scenario you read in just a portion of the file, make the adjustments within that portion, and then write that protion back to file. The portions you read in/write out need to be the same size or else you need to have some mechanism to determine where each portion of the file starts. Some might argue that each portion of the file constitutes a file in and of itself, and I wouldn't have a good come back for that.

Having your program being able to read and write from a file makes it more powerful in my opinion. If your ATM program could read from files then you could have multiple accounts and not just one predefined account, then it would be very versitile and work more like a real ATM machine.

You could start your program with predefined accounts, but then you will have to go back and change it later to make it read from files if that is what you are required to do. This can simply be inneficient.

This article has been dead for over six months. Start a new discussion instead.