Thread: Storing Data into a file

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    36

    Exclamation Storing Data into a file

    I have created my own class called Account which can be used to make a saving account, deposit/ widthdraw money, etc...

    First the program asks for a User ID and password. Currently, I have made it, so that one can enter anything and still continue the program.

    In order to simulate a real bank, I have to create a database or something that will allow the user to create a User ID and Password and then, that information will be stored into a file of some sort. Then he/she will enter their current balance, make any transactions and 'log out.' The next time they use the program, I want to make it so that they can log in to their own account they have created earlier and continue on from there.

    Right now, in my program, everytime it runs,the user must start over from the beginning.

    My question is, "How can I create a database file that will store the information the user enters when he/she creates a new account?

    Note that I will add the functions, "Create()" and "Destroy()" to the class later on.

    Here is what the proram looks like as of now: Note that the utility library is something that i created and it contains useful functions such as GetDouble(), cls(), etc...

    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>
    
    
    using namespace std;
    
    //------------------------Prototypes and Constants------------------------------
    
    const double Interest=0.03;
    const double Natural_E=2.712;
    
    //-----------------------User-defined Account Class-----------------------------
    class Account
    {
          private:  //for the private variables and methods
                  double Balance;
                  double Original;
                  double DepositMoney;
                  double WidthdrawMoney;
                  
                  int Years;              
                  int Code;
                  
                  string Name;
                                        
                  double InterestPay(int Years)
                  {
                         double temp,temp2;
                         
                         temp=Interest*Years;
                         temp2=pow(Natural_E,temp);
                         
                         
                         return Balance*temp2;
                  }
                  
                  
                  
          public: //for public variables and methods
                 Account();
                 ~Account();
                 
                 char AskAns;
                 char Ask();
                 
                 char Deposit_Ask;
                 char DepositAns();
                 
                 char Widthdraw_Ask;
                 char WidthdrawAns();
                 
                 void InputCurrentBalance();
                 void Deposit();
                 void Widthdraw();
                 void GetBalance();
                 void ListTransaction();
                 void Instructions();
                 void NumYears(int &Years);
                 void AskName();
                 void InterestAdd();
                 
                 
    };
    
    //-----------------Constructor and Methods and Destructor-----------------------
    
    Account::Account() //Constructor
    {       
    			/*Sets everthing equal to 0*/
    
                      Balance=0;
                      DepositMoney=0;
                      WidthdrawMoney=0;
                      Years==0;
    }
    
    Account::~Account(){}
    
    void Account::GetBalance()
    {
         cout<<"Your balance as of now is $"<<Balance<<endl;
    }
    
    void Account::Instructions() //Postcondition:Displays intructions
    {							//PreCondition: none
    	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();
    	
    	cls();
    }
    void Account::InputCurrentBalance() //Postcondition:entering current balance
    {									//Precondition: Nothing
         
         cout<<"Enter the current balance on your account now in dollars: $";
         cin>>Balance;
         Original=Balance;
         
    }
    
    void Account::Deposit() //Postcondition:Method for depositing money
    {						//Precondition: Nothing Any $ amount can be put in
         cout<<"How much do you want to deposit into your account?: $";
         DepositMoney=GetDouble(0,1000000000); // for illegal transactions
         Balance+=DepositMoney;
         //Balance+=InterestPay(Years); //pays interest
         cout<<"The new balance with the interest added now is: $"<<Balance<<endl;
         
    }
    
    void Account::Widthdraw() //Postcondition:Method for taking money from bank
    						//PreCondition: Money can't be more than balance
    						
    {
         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(2500);
    }
    
    
    void Account::ListTransaction()  //Postcondition:Prints the transaction
    {								//PreCondition: Nothing
    	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;
    	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()  //Postcondition:Asks if program should run again
    {					//No Precondition
    	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::AskName()
    {
         
         cout<<"Please enter in your User ID: ";
         cin>>Name;
         cout<<"Enter your 5 digit access number provided by Sridar: ";
         cin>>Code;
         cout<<"Thank You!\n";
         cout<<"Press ENTER to proceed\n";
         getch();
         cls();
    }
    
    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;
    }
    
    
    //-------------------------------------Main--------------------------------
    int main()
    {
    	
    	cout.setf(ios::fixed); cout.precision(2); //Rounds everything to 2 decimal  places
    	
     newuser:
    	
        Account X; //creating an instance of the class	
    	X.Instructions();
    	X.AskName();    
        
            	
        Account Sridar;//main instance of Account class    
        
        Sridar.InputCurrentBalance();
        
     start:
        
        int years;
        char Answer;
        
        Sridar.InterestAdd();    
        
        if(Sridar.DepositAns()=='y')
        {
                                    Sridar.Deposit();
                                    }
        if(Sridar.WidthdrawAns()=='y')
        {
                                    Sridar.Widthdraw();
                                    }
        
        
        Sridar.ListTransaction();
                                      
        Answer=Sridar.Ask();
       
        if(Answer=='Q')
        {
        	cout<<"Have a nice day!\n";
        	getch();
        	cls();
        }
        
        if(Answer=='A')
        {
        	cout<<"Okay! Press Enter now\n";
        	getch();
        	cls();
        	goto start;
        }
        
        if(Answer=='N')
        {
             cout<<"Logging Out...";
             Wait(2000);
             cls();
             goto newuser;
             }
        
        return 0;
    }
    Please Help ME!
    Last edited by Sridar; 05-29-2005 at 12:15 PM.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Storing data to a file couldn't be simpler.

    Read up on 'fstreams' there should be a tutorial on this site.
    Once you've done that try giving your code another bash.


  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes, and once you have learned the mechanics of file i/o, you'll wonder how you're going to store the information. By the looks of your program, the simplest way would just to have the program save information in a definite order, line by line. Of course, in the case of multiple accounts, things could get stickier. Most extravagantly, you could look into an XML parser/coder.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  3. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:20 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM