I am having a little trouble using fstream. Currently i am working on a project which simulates a bank. The thing is, i have a file that lists the current balances of the accounts. For example, here is a file:
************
25000
15000
5000
2000
************
This shows that the user whose index number is one has a balance of $25000. My program simulates interest and transactions. However, at the end of the program, that balance changes. My problem is that i do not know how to change the first line of the program so that it reads a different number.
Say, i am the 3rd user to sign up for an account, i would have an index number of 3 and i have $5000. I widthdraw and deposit money so that i now have a current balance of say $9875. How do i edit the file so that the $5000 is overwritten by '9875'?
Here is the code for such a program
I also have another question. when instantiating an object of fstream, i can use:Code:/* Name: The Bank Copyright: 2005 Author: Sridar Ravi Date: 28/05/05 16:27 Description: A Bank */ #include<iostream> #include<conio.h> #include<utility.h> #include<math.h> #include<fstream> using namespace std; //------------------------Prototypes and Constants------------------------------ const double Interest=0.03; const double Natural_E=2.712; ofstream NameFile("Name.txt", ios::app); ofstream PINFile("PIN.txt", ios::app ); ofstream TransFile("Trans.txt", ios::app); ofstream CurrentBalFile("CurrentBal.txt",ios::); ofstream DateFile("Date.txt", ios::app); ofstream StartFile("Start.txt", ios::app); //-----------------------User-defined Account Class----------------------------- class Account { private: double Balance; double Original; double DepositMoney; double WidthdrawMoney; int Years; int Code; string Name; string NewName; double NewPIN, StartingMoney; double InterestPay(int Years) { double temp,temp2; temp=Interest*Years; temp2=pow(Natural_E,temp); return Balance*temp2; } public: Account(); ~Account(); char AskAns; char Ask(); char choice; void Choice(char &choice); char Deposit_Ask; char DepositAns(); char Widthdraw_Ask; char WidthdrawAns(); void CurrentBalance(); void Deposit(); void Widthdraw(); void GetBalance(); void ListTransaction(); void Instructions(); void NumYears(int &Years); void Login(); void InterestAdd(); void Create(); void Delete(); int index; }; //-----------------Constructor and Methods and Destructor----------------------- Account::Account() //Constructor { /*Sets everthing equal to 0*/ Balance=0; DepositMoney=0; WidthdrawMoney=0; Years==0; index=0; } Account::~Account(){} void Account::GetBalance() { cout<<"Your balance as of now is $"<<Balance<<endl; } void Account::Instructions() { cout<<"Welcome to The Bank of Sridar Ravi!\n\n"; cout<<"This bank only allows for savings accounts at this time!\n\n"; cout<<"Checking accounts and money market accounts will become available soon\n\n"; cout<<"This bank allows you to enter in your initial balance\n"; cout<<"and then you will wait for the money to grow\n\n"; cout<<"You can also make transactions if you wish\n\n\n\n"; cout<<"To proceed, please press ENTER now\n"; getch(); system("cls"); } void Account::CurrentBalance() { double ref2; int q; ifstream CurrentBalFile("CurrentBal.txt"); for(q=1;q<=10000;q++) //Finds Balance based on index number { CurrentBalFile>>ref2; if(q==index) { break; } } Balance=ref2; Original=Balance; cout<<"Your current balance is $"<<Balance<<endl; cout<<"Press ENTER to proceed\n"; getch(); system("cls"); } void Account::Deposit() { cout<<"How much do you want to deposit into your account?: $"; DepositMoney=GetDouble(0); Balance+=DepositMoney; cout<<"The new balance with the interest added now is: $"<<Balance<<endl; } void Account::Widthdraw() { cout<<"How much would you like to widthdraw?: $"; WidthdrawMoney=GetDouble(0,Balance); Balance-=WidthdrawMoney; cout<<"Please wait while we process you new balance...\n"; Wait(1500); } void Account::ListTransaction() { cout<<"\n\n"; cout<<"Your Transaction\n\n"; cout<<"Your original balance was: $"<<Original<<"\n"; cout<<"You deposited: $"<<DepositMoney<<endl; cout<<"You widthdrew: $"<<WidthdrawMoney<<endl; cout<<"The new balance is: $"<<Balance<<endl; for(int w=1; w<=index; w++) { if(w==index) { CurrentBalFile<<Balance; } } cout<<"This bank gives you an interst rate of "<<Interest*100<<"% compounded continuously\n"; cout<<"We hope you are content with the transaction!\n"; } char Account::Ask() { cout<<"What would you like to do now? \n"; cout<<"A---------Another Transaction\n"; cout<<"N---------New User\n"; cout<<"Q---------Quit\n"; cout<<"Please enter one of the choices: "; cin>>AskAns; return AskAns; } void Account::NumYears(int &Years) { cout<<"How many years has passed since the last transaction or starting this account?: "; cin>>Years; } void Account::Login() { cout<<"Please enter in your User ID: "; cin>>Name; repeat_code: cout<<"Enter your access number: "; cin>>Code; string temp="a"; int ref; ifstream NameFile("Name.txt"); ifstream PINFile("PIN.txt"); for(index=1;temp!=Name;index++) //searches for the Name to get index number { NameFile>>temp; if(temp==Name) { break; } } for(int i =1; i<=10000;i++) // finds the PIN corresponding to index { PINFile>>ref; if(index==i) { break; } } if(Code==ref) //checks to see if PIN is good { cout<<"Thank You!\n"; } else { cout<<"Invalid PIN!! Enter again....\n"; goto repeat_code; } cout<<"You are now logged in. Pess any key to continue...\n"; getch(); } void Account::InterestAdd() { NumYears(Years); Balance=InterestPay(Years); //pays interest cout<<"The new balance with the interest added now is: $"<<Balance<<endl; } char Account::WidthdrawAns() { cout<<"Would you like to widthdraw?: "; cin>>Widthdraw_Ask; return Widthdraw_Ask; } char Account::DepositAns() { cout<<"Would you like to deposit?: "; cin>>Deposit_Ask; return Deposit_Ask; } void Account::Create() { system("cls"); cout<<"Enter the User ID of the account that you wish to make: "; cin>>NewName; NameFile<<NewName<<endl; cout<<"Now, please enter the PIN number that you want to create: "; cin>>NewPIN; PINFile<<NewPIN<<endl; cout<<"How much money would you like to put into this account?: "; cin>>StartingMoney; StartFile<<StartingMoney<<endl; CurrentBalFile<<StartingMoney<<endl; cout<<"Congratulations! Your account is now created\n"; cout<<"Please press ENTER to end this program, so that you can access your account\n"; getch(); } void Account::Choice(char &choice) { cout<<"What would you like to do?\n"; cout<<"A-------Create new account\n"; cout<<"B-------Log In\n"; cout<<"C-------Delete Account\n"; cout<<"Your Choice: "; cin>>choice; } void Account::Delete() { cout<<"You are about to delete this account....\n"; cout<<"Your account has been deleted.\n"; cout<<"Press Enter to end this program now\n"; getch(); } //-------------------------------------Main-------------------------------- int main() { cout.setf(ios::fixed); cout.precision(2); //Rounds everything to 2 decimal places newuser: Account X; X.Instructions(); char choice; X.Choice(choice); if(choice=='a') { X.Create(); return 0; } else if(choice=='b') { X.Login(); } else if(choice=='c') { X.Delete(); return 0; } X.CurrentBalance(); start: int years; char Answer; X.InterestAdd(); if(X.DepositAns()=='y') { X.Deposit(); } if(X.WidthdrawAns()=='y') { X.Widthdraw(); } X.ListTransaction(); Answer=X.Ask(); if(Answer=='Q') { cout<<"Have a nice day!\n"; getch(); system("cls"); } if(Answer=='A') { cout<<"Okay! Press Enter now\n"; getch(); system("cls"); goto start; } if(Answer=='N') { cout<<"Logging Out..."; Wait(2000); system("cls"); goto newuser; } return 0; }
but i didn't use that cause i want to read and write to the file and append to it when i am writing to the file. The following code above does not do that. Thus, i have to put:Code:fstream MyFile("myfile.txt", ios::in | ios::out | ios::app)
this outside of main, and theCode:ofstream MyFile("myfile.txt", ios::app)
inside main or in the functions. I can combine them, bu te results are not what i want.Code:ifstream MyFile("myfile.txt")
Help me out here....



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.