1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ programHelp

  1. Oct 30, 2006 #1
    C++ program..Help!!!!

    Well i am working on an online banking system.Here is my code(not so ood but its a start)

    #include <iostream>
    #include <string>
    using namespace std;

    void header()
    {
    cout<<"\t\t\tWELCOME TO ONLINE BANKING SYSTEM"<<endl;
    cout<<endl;
    };

    int getacctno();
    int checkaccno(int );

    class User
    {
    public:
    char login[10];
    int pw;

    User()
    {
    strcpy(login,"");
    pw=0;
    }

    void start()
    {
    cout<<"\tPlease Enter your login and password"<<endl;
    cout<<"\t\tLogin:\t";
    cin>>login;
    cout<<" "<<endl;
    cout<<"\t\tPassword:\t";
    cin>>pw;
    cout<<" "<<endl;

    }

    void logout()
    {
    strcpy(login," ");
    pw=0;
    cout<<"You have logged out successfully.\n\n";
    start();
    } //end of logout function

    }; //end of class User

    void logout(User );



    class Customer:public User
    {
    public:
    char Name[50];
    int AcctNo;
    char Address[150];
    int Tel;
    int CurrentBalance;

    Customer():User()
    {
    strcpy(login,"");
    pw=0;
    strcpy(Name,"");
    AcctNo=1;
    strcpy(Address,"");
    Tel=0;
    CurrentBalance=0;
    }
    Customer(User A):User()
    {
    strcpy(login,"cust");
    pw=1234;
    strcpy(Name,"abd def");
    AcctNo=0;
    strcpy(Address,"H.no 101, abc street 31, Rawalpindi");
    Tel=12345678;
    CurrentBalance=5000;
    }

    void custmenu()
    {
    cout<<"\n\t\tWELCOME to CUSTOMER MENU\n\n";
    cout<<"\t\t\t1. Display information"<<endl;
    cout<<"\t\t\t2. Cash Transfer"<<endl;
    cout<<"\t\t\t3. Withdraw Cash"<<endl;
    cout<<"\t\t\t4. Display Balance"<<endl;
    cout<<"\t\t\t5. Change password"<<endl;
    cout<<"\t\t\t6. Log out"<<endl;
    cout<<"\t\tEnter choice(1-6):";
    int choice;
    cin>>choice;

    if (choice==1)
    {
    display();
    custmenu();
    }

    else if (choice==2)
    {
    transfer();
    }

    else if (choice==3)
    {
    withdraw();
    }

    else if (choice==4)
    {
    displayBalance();
    }

    else if (choice==5)
    {
    changepw();
    }

    else if (choice==6)
    {
    logout();
    }

    }

    void display()
    {
    cout<<"\tCustomer Information:"<<endl;
    cout<<"\t\tName: "<<Name<<endl;
    cout<<"\t\tAccount No.: "<<AcctNo<<endl;
    cout<<"\t\tAddress: "<<Address<<endl;
    cout<<"\t\tTelephone no.: "<<Tel<<endl;
    cout<<"\t\tCurrent balance:Rs. "<<CurrentBalance<<endl;
    }

    void transfer()
    {
    int am;
    long accno;
    cout<<"\tCash Transfer :"<<endl;
    cout<<"Amount to tranfer :Rs.";
    cin>>am;

    if (am<CurrentBalance && CurrentBalance<100)
    {
    cout<<"No. of Account where money is to be tranfered :";
    cin>>accno;
    }
    else if (am>CurrentBalance)
    {
    cout<<"Amount of transfer is greater than Current Balance of your account."<<endl;
    transfer();
    }

    cout<<"\tTransfer of Rs."<<am<<" to acoount no. "<<accno<<" was successful."<<endl;

    CurrentBalance=CurrentBalance-am;

    /*/list[accno].CurrentBalance=list[accno].CurrentBalance+am;
    This isnt working as list of customer must be defined after class definition
    and function cannot be placed out of class as funtion prototype containing the
    argument of type Customer will have to be placed before class defintion./*/

    cout<<"\tNow, the Balance of your account is Rs."<<CurrentBalance<<endl;
    custmenu();
    }

    void withdraw()
    {
    int wd;
    cout<<"Please enter the amount to withdraw :Rs. ";
    cin>>wd;
    if (wd<CurrentBalance && CurrentBalance-wd>100)
    {
    CurrentBalance=CurrentBalance-wd;
    cout<<"Withdrawn amount: "<<wd;
    }
    else if (wd>CurrentBalance || CurrentBalance-wd<100)
    {
    cout<<"Cannot Withdraw!!Please check your Current Balance."<<endl;
    }
    custmenu();
    }

    void displayBalance()
    {
    cout<<"Your account balance is Rs."<<CurrentBalance<<endl;
    custmenu();
    }

    void changepw()
    {
    int newpw;
    cout<<"Please enter ur new password : ";
    cin>>newpw;
    pw=newpw;
    cout<<"Password changed successfully.\n";
    custmenu();
    }


    }; //end of class Customer
    Customer list[10];

    class Administrator:public User
    {
    public:
    Administrator():User()
    {
    strcpy(login,"admin");
    pw= 123456;
    } //end of default constructor

    void adminmenu()
    {
    cout<<"\n\n\t\tWELCOME to ADMINISTRATOR MENU\n\n";
    cout<<"\t\t\t1. Create New Account"<<endl;
    cout<<"\t\t\t2. Delete Existing Account"<<endl;
    cout<<"\t\t\t3. Update login data"<<endl;
    cout<<"\t\t\t4. Search for Account"<<endl;
    cout<<"\t\t\t5. Add balance"<<endl;
    cout<<"\t\t\t6. Deduct balance"<<endl;
    cout<<"\t\t\t7. View All Accounts"<<endl;
    cout<<"\t\t\t8. Logout"<<endl;
    cout<<"\t\tEnter choice(1-8):";
    int choice;
    cin>>choice;

    if (choice==1)
    {createacct();
    adminmenu();}

    if (choice==2)
    {delacct();}

    if (choice==3)
    {update();}

    if (choice==4)
    {search();}

    if (choice==5)
    {addbalance();
    adminmenu();}

    if (choice==6)
    {dedbalance();
    adminmenu();}

    if (choice==7)
    {viewall();
    adminmenu();}

    if (choice==8)
    {logout();}
    }

    void createacct();
    void delacct();
    void check(char[10] );
    void update();
    void search()
    {
    cout<<"Enter account number to be searched :";
    int acct;
    cin>>acct;
    int n=checkaccno(acct);
    if (n==0)
    {
    list[acct].display();
    adminmenu();
    }
    else
    { cout<<"The account number "<<acct<<" doesnt exist\n";
    search();
    }
    }
    void addbalance()
    {
    cout<<"\tEnter account number to which balance is to be added : ";
    int acct,amt;
    cin>>acct;
    cout<<"\tEnter the balance: ";
    cin>>amt;
    int n=checkaccno(acct);
    if (n==0)
    {
    list[acct].CurrentBalance=list[acct].CurrentBalance+amt;
    cout<<"Account updated successfully: \n\tThe current balance is:"<<list[acct].CurrentBalance;
    }
    else
    { cout<<"The account number "<<acct<<" doesnt exist\n";}
    }

    void dedbalance()
    {
    cout<<"\tEnter account number from which balance is to be deducted : ";
    int acct,amt;
    cin>>acct;
    cout<<"\tEnter the balance: ";
    cin>>amt;
    int n=checkaccno(acct);
    if (n==0)
    {
    list[acct].CurrentBalance=list[acct].CurrentBalance-amt;
    cout<<"Account updated successfully: \n\tThe current balance is:"<<list[acct].CurrentBalance;
    }
    else
    { cout<<"The account number "<<acct<<" doesnt exist\n";}
    }

    void viewall()
    {
    for(int i=0;i<11;i++)
    {
    list.display();
    }
    }



    }; //end of class Administrator

    void Administrator::createacct()
    {
    char Name[50];
    char Address[150];
    int Tel;
    int CurrentBalance;
    cout<<"Enter login for new account : ";
    cin>>login;
    check(login);
    cout<<"Enter password for new account : ";
    cin>>pw;
    int n=getacctno();
    strcpy(list[n].login,login);
    list[n].pw=pw;
    list[n].AcctNo=n;
    cout<<"\nEnter personal details of customer : ";
    cout<<"\n\t\tName: ";
    cin>>Name;
    strcpy(list[n].Name,Name);
    cout<<"\n\t\tAddress: ";
    cin>>Address;
    strcpy(list[n].Address,Address);
    cout<<"\n\t\tTelephone no.: ";
    cin>>Tel;
    list[n].Tel=Tel;
    cout<<"\n\t\tCurrent balance:Rs. ";
    cin>>CurrentBalance;
    list[n].CurrentBalance=CurrentBalance;
    cout<<"You have created a new account successfully."<<endl;
    cout<<"The account no. of new account is "<<n;

    }

    void Administrator::delacct()
    {
    int accno;
    cout<<"Enter account number you want to delete : ";
    cin>>accno;
    int n=checkaccno(accno);
    if (n==0)
    { cout<<"The account "<<accno<<" is deleted.\n";}
    else
    { cout<<"The account number "<<accno<<" doesnt exist\n";
    delacct();
    }
    adminmenu();
    }

    void Administrator::check(char login[10])
    {
    for(int i=0;i<11;i++)
    {
    if (strcmp(list.login,login)==0)
    {
    cout<<"Login unavailable.Please enter another.\n";
    createacct();
    }
    }

    }
    void Administrator::update()
    {
    cout<<"\tEnter right choice:\n";
    cout<<"\t\t1. Change password\n\t\t2. Enable disable account:";
    int choice;
    cin>>choice;
    if (choice==1)
    {
    cout<<"Enter the account number whose password is to be changed: ";
    int acctno;
    cin>>acctno;
    int n=checkaccno(acctno);
    if (n==0)
    { cout<<"Enter new password for account : ";
    int newpw;
    cin>>newpw;
    list[acctno].pw=newpw;
    cout<<"\tAccount updated.\n";
    }
    else
    { cout<<"The account number "<<acctno<<" doesnt exist\n";
    update();
    }
    adminmenu();
    }
    if (choice==2)
    {
    cout<<"Enter the account number you want to Enable or Disable: ";
    int acct;
    cin>>acct;
    int n=checkaccno(acct);
    if (n==0)
    { cout<<"Enter 1 to enable and 2 to disable account "<<acct;;
    int letter;
    cin>>letter;
    if (letter==1)
    {
    cout<<"\tAccount enabled.\n";
    }
    else if (letter==2)
    {
    cout<<"\tAccount disabled.\n";
    }
    else
    {
    cout<<"Enter valid choice.\n";
    update();
    }
    }
    else
    { cout<<"The account number "<<acct<<" doesnt exist\n";
    update();
    }
    }
    }


    int main()
    {
    header();
    User A;
    Administrator Ad;
    Customer Cust1;
    Customer Cust(A);
    A.start();

    if (strcmp(A.login,Ad.login)==0 && A.pw==Ad.pw)
    {
    cout<<"You have signed in successfully as Administrator."<<endl;
    Ad.adminmenu();
    }
    else if (strcmp(A.login,Ad.login)==0 && A.pw!=Ad.pw)
    {
    cout<<"Wrong password for Administrator account."<<endl;
    A.start();
    }
    else if (strcmp(A.login,Ad.login)!=0 && strcmp(A.login,Cust.login)!=0)
    {
    cout<<"Enter valid user name."<<endl;
    A.start();
    }

    else if(strcmp(A.login,Cust.login)==0 && A.pw==Cust.pw)
    {
    cout<<"You have signed in successfully as Customer."<<endl;
    Cust.custmenu();
    }
    else if(strcmp(A.login,Cust.login)==0 && A.pw!=Cust.pw)
    {
    cout<<"Wrong password for Customer account."<<endl;
    A.start();
    }


    return 0;
    } //end of main

    int getacctno()
    {
    for (int i=0;i<11;i++)
    {
    if (strcmp(list.Name,"")==0)
    {return i+1;}
    else
    {cout<<"No account available\n";}
    }
    }

    int checkaccno(int accno)
    {
    if (accno>-1 && accno<11)
    { return 0;}

    }


    Well the question is
    Look at the create account function of class Administrator which calls getacctno() function. I am having problem assigning the account number to new account using this function, it returns same value always.
    Also how can i enable nd disable an account.
     
  2. jcsd
  3. Oct 30, 2006 #2
    Ahhh, please make simplier

    No one is going to debug many lines of uncommented code for you.

    I would suggest commenting the code first. Say what it is doing and why. There is a good chance you will see the problem while doing that exercise. You should have done that anyway while writing the code.

    If you have it well commented and still don't see the problem, put a break point in the method of interest and check the value of each variable carefully as you step throught the function. You should see the problem...
     
  4. Oct 30, 2006 #3
    I agree with learner, thats alot of uncommented code. ALso why don't you just use a switch statement for all those if's?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ programHelp
  1. C program help (Replies: 5)

  2. C++ program help! (Replies: 1)

  3. C program help needed (Replies: 3)

Loading...