1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++, simple bank simulation. devc++ compiler STOPS halfway.

Discussion in 'C++' started by jose_peeterson, Jul 8, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    // dear experts, i have no idea where the error is, i don't get any error messages but //after running it
    // stops half way, please rectify this problem, thanks.
    // there is No error in code.


    #include<iostream.h>
    using namespace std;

    class bankacct
    {
    private : int acc_num ;
    double bal;

    public : bankacct()
    {
    acc_num = 20110001;
    bal = 0;
    }
    void Newaccount(int num);
    void deposit(double amount);
    void withdrawal(double amount);
    void print(int num, class bankacct account[5]);
    };

    void bankacct::Newaccount(int num)
    {
    cout<<"Your choice 1\n\n";
    acc_num = acc_num + num;
    cout<<"Result: ["<<num<<"/5 account created.\n";
    cout<<"Account Number: "<<acc_num<<"\n";
    cout<<"Balance: $0.00\n";
    return;
    }

    void bankacct::deposit(double amount)
    {
    cout<<"Your choice: 2\n";
    cout<<"Account Number: "<<acc_num<<"\n";
    cout<<"Amount: "<<amount<<"\n\n";

    cout<<"Result: Deposit performed\n";
    cout<<"Account Number: "<<acc_num<<"\n";
    cout<<"Balance: $"<<(bal = (bal + amount))<<"\n";
    return;
    }

    void bankacct::withdrawal(double amount)
    {
    if(amount > bal)
    {
    cout<<"Withdrawal failed, withdrwal greater than account balance\n";
    return;
    }

    cout<<"Your choice: 3\n";
    cout<<"Account Number: "<<acc_num<<"\n";
    cout<<"Amount: "<<amount<<"\n\n";

    cout<<"Result: Withdrawal performed\n";
    cout<<"Account Number: "<<acc_num<<"\n";
    cout<<"Balance: $"<<(bal = (bal - amount));
    return;
    }

    void bankacct::print(int num, class bankacct account[5])
    {
    int ctr;

    cout<<"Result: "<<num<<" existing accounts.\n";
    for(ctr=0;ctr<num;ctr)
    {
    cout<<"Account number: "<<account[ctr].acc_num<<"\n";
    cout<<"Balance: $"<<account[ctr].bal<<"\n\n";
    }
    return;
    }

    int main()
    {
    class bankacct account[5];
    double amount;
    int num = 20110000; // user number 0 - 4, account[num];
    int operation = 0;

    while((operation != 5) || (num > 4))
    {
    cout<<"Service available (1. New Acct, 2. Deposit, 3. Withdrawal, 4. Print All, 5. exit)\n";
    cin>>operation;

    if(operation == 1)
    num++;

    if(operation == 1)
    {
    account[num].Newaccount(num);
    }

    if(operation == 2)
    {
    cout<<"Enter the amount to deposit : ";
    cin>>amount;
    account[num].deposit(amount);
    }

    if(operation == 3)
    {
    cout<<"Enter the amount to withdraw : ";
    cin>>amount;
    account[num].withdrawal(amount);
    }
    if(operation == 4)
    {
    account[1].print(num,account);
    }
    }

    if(num>4)
    {
    cout<<"Sorry, not more than 5 accounts in 1 attempt\n";
    return 0;
    }

    system("pause > null");
    return 0;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You've been around here long enough to know by now: USE CODE BLOCKS WHEN POSTING CODE. It's REALLY not that hard. Read the posting guidelines if you've forgotten how.

    >> there is No error in code

    Are you really certain about that? Have a good think about the following lines:
    Code:
    class bankacct account[5];
    int num = 20110000;
    account[num].Newaccount(num);
    
     
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    mr xipittos,
    thank you for being meticulous enough to detect silly errors. i got my program finished and working. but i dont get what you mean by code blocks. you dont have to reply me now
    tell me in my answer to next question.
    thanks.

    other errors include ctr (ctr++) num (num+1) num=0 (num=-1)
     
  4. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    //sorry one more thing,
    //in the previous program when i wanted to print the accounts. i had to do this

    if(operation == 4)// how to print all existing accounts??
    {
    account[1].print(num,account);
    }

    //here i randomly chose account[] to be 1. but is there any standard way to call this function.
    // i think my programing style is not good. i should have printed each account individually but using a for loop in the main itself. RIGHT?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    When you press New Topic to start a new thread, there's a bit of text that says

    Before proceeding, please make sure you are aware of the Community Guidelines and you have read the Hints before you make a post thread.

    It's between the title box and the main message box, you can't really miss it. The explanation of code blocks is in one of those two so please read them both in full.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    About printing the accounts, you don't really seem to have got a handle on this object-oriented thing. A bankacct object should represent just one bank account, not the whole collection, so the print function should display only one account, i.e. "this" one. So you'd end up with something like:
    Code:
    void bankacct:print()
    {
    cout<<"Account number: "<<acc_num<<"\n";
    cout<<"Balance: $"<<bal<<"\n\n";
    }
    
    //...
    
    if (operation==4)
    {
    int ctr;
    
    cout<<"Result: "<<num<<" existing accounts.\n";
    for(ctr=0;ctr<num;ctr)
      account[ctr].print();
    }
    
     

Share This Page