Data Reading and Displaying....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhinuke
    New Member
    • May 2010
    • 23

    Data Reading and Displaying....

    OK...Here's the deal, I implemented a class for banking application with simple functions like

    1.Withdrawal
    2.Deposit
    3.Balance Inquiry
    4.Creation of Account.


    I have this code with me and it works fine until I create individual account,after which it is entering infinite loop.I checked the code and its not givin me any clues for error whatsoever.

    I plan to create account and then perform above functions but its just not running properly.I also need suggestions for any improvements regarding string usage and class implementation.

    Do I need to use file to read the account details? Or it will work without it.

    Here's the code below.

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    using std::cout;
    using std::endl;
    using std::string;
    class account
    {
     string name;
     string type;
        int acc_no;
        float bal;
    public:
        void create(string &n,string &t,int an,float bl)
        {
            name.assign(n);
            type.assign(t);
            
            acc_no=an;
            bal=bl;
        }
          void deposit(float dep)
        {
            bal=bal+dep;
            cout<<"You deposited "<<dep<<"and your new balance is "<<bal;
        }
       void withd(float dep)
       {
           bal=bal-dep;
           cout<<"Your withdrawl amount is "<<dep<< " and your balance is "<<bal;
       }
       void display(void);
    };
    void account::display(void)
    {
        cout<<"Name "<<name<<endl;
        cout<<"Balance "<<bal;
    }
    int main()
    {
        account ac;
        string a_name;
        string a_type;
      //  char c;
        int a_no,ch;
        float a_bal;
    
    //    cout<<"Do you have an account in bank? Y/N\n";
    //    cin>>c;
    //    if(c=='N')
    //    {
        cout<<"Enter your details in following manner\n";
        cout<<"Name of the account holder \t";
        cin>>a_name;
        cout<<"Type of the account whether savings or current \t ";
        cin>>a_type;
        cout<<"Account Number \t";
        cin>>a_no;
        cout<<"Initial Balance \t";
        cin>>a_bal;
        ac.create(a_name,a_type,a_no,a_bal);
        cout<<"Your account has been created!!!";
    //    }
    
    //    else
    //    {
    
            cout<<"Enter your options as follows\n";
            cout<<"1.Deposit\n";
            cout<<"2.Withdrawl\n";
            cout<<"3.Balance Enquiry";
        while(ch!=4)
    
        {
            switch(ch)
            {
                case 1:
          
                    cout<<"Enter amount to be deposited\n";
                    cin>>a_bal;
                            ac.deposit(a_bal);
                            break;
           
                case 2:
             
                    cout<<"Enter amount to be withdrawled";
                    cin>>a_bal;
                            ac.deposit(a_bal);
                            break;
              
                case 3:
                          
                    ac.display();
                    break;
                        
                case 4:
    
                    exit(0);
    
                default:
                    cout<<"Please enter proper option!!";
    
                
    //        }
        }
        }
    
        return 0;
        
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need some design here.

    First, your user interface is buried in the acocunt class. The user interface should be in main() and not in the class. That is, all of your displays need to be in main() and not in the acocunt class.

    Second, your create function shoul return a pointer to the object it created.
    That is, your main() should look like:

    Code:
    int main()
    {
       account* obj = createAccount(SAVINGS)
       accunt* obj1 = createAccunt(CHECKING);
    
    }
    To make this work you will need a class hierarchy for account. The derived classes are checkingAccount and savingsAccount. The createAccount function is a factory function. It uses the argument to determine which type of derived object to create and passes back the address of that object as a base class pointer.

    That is, you will need a little polymorphism here.

    That will mean a design of the account class interface.

    But what ever you do, you cannot write an entire program as one class. That's what you do in C. If you look ar your account class it really is a main().

    I always tell people to write the code for main() and then write the classes to support that code. This way your classes contain only the minimum number of features to make main() work. If main() requires more features later then add them later.

    Comment

    Working...