Hi friends, I am new to c++, just find this example from the book, but when i try to run in my visual studio,its gives problem.
there is no error in the codes, it compile well, and in main screen ask the user to input the selection that they want, after user input the selection, its just terminate the program,
can any of u fixed or help me on this,
thanks friend.

this code is the problem, when user enters anything based on this screen, its just, terminate the program.

 cout<<"\nTraction type of availalble";
            cout<<"\nO - open account";
            cout<<"\nD - deposit";
            cout<<"\nW - withdraw";
            cout<<"\nB - Balance";
            cout<<"\nX - Exit";
            cout<<"\nEnter choice :";
            cin>>transaction_type;
#include<iostream>
#include<ctype.h>

using namespace std;

class current
{
    int account_no;
    double balance;

public :

    void open_account();
    void deposit();
    void withdraw();
    void show_balance();

};

void current::open_account()
{
    cout<<"\nOpen current account....";
    cout<<"\n Enter account number :";
    cin>>account_no;
    cout<<"\n Enter starting amount :";
    cin>>balance;
}
void current::deposit()
{
    int acct_no;
    double amount;
    cout<<"\nDeposit into current account...";
    cout<<"\nEnter account number :";
    cin>>acct_no;

    if(acct_no != account_no)
    {
        cout<<"\n invalid account.Please try again";
    }
    else
    {
        cout<<"\nEnter amount to deposit : $";
        cin>>amount;
        balance =balance + amount;
        cout<<"\n Your new balance is $"<<balance;
    }
}

void current ::withdraw()
{
    int acct_no;
    double amount;
    cout<<"\nWithdraw from current account...";
    cout<<"\nEnter account number :";
    cin>>acct_no;

    if(acct_no != account_no)
    {
        cout<<"\n invalid account.Please try again";
    }
    else
    {
        cout<<"\nEnter amount to withdraw : $";
        cin>>amount;
        if(amount > balance)
        {

        cout<<"\n sorry, insufficient balance";
        }
        else
        {
            cout<<"\nPlease take your cash";
            balance = balance - amount;
            cout<<"\n Your new balance is $"<<balance;
        }
    }
}

void current::show_balance()
{
    int acct_no;
    cout<<"\n show current account balance ";
    cout<<"\n Enter your account number :";
    cin>>acct_no;

    if(acct_no ==account_no)
        cout<<"\n Your balance is S "<<balance;
    else
        cout<<"\n Invalid account. Please try again";
}

class saving
{
    int account_no;
    double balance;

public:
    void open_account();
    void deposit();
    void withdraw();
    void show_balance();
};

void saving::open_account()
{
    cout<<"\nOpen current account....";
    cout<<"\n Enter account number :";
    cin>>account_no;
    cout<<"\n Enter starting amount :";
    cin>>balance;
}

void saving ::deposit()
{
    int acct_no;
    double amount;
    cout<<"\nDeposit into saving account...";
    cout<<"\n Saving account deposit";
    cout<<"\nEnter account number :";
    cin>>acct_no;

    if(acct_no != account_no)
    {
        cout<<"\n invalid account.Please try again";
    }
    else
    {
        cout<<"\nEnter amount: $";
        cin>>amount;
        balance =balance + amount;
       // cout<<"\n Your new balance is $"<<balance;
    }
}
void saving ::withdraw()
{
    int acct_no;
    double amount;
    cout<<"\nWithdraw from saving account...";
    cout<<"\nEnter account number :";
    cin>>acct_no;

    if(acct_no == account_no)
    {
        cout<<"\nEnter amount to withdraw : $";
        cin>>amount;
        if(amount > balance)
        {

        cout<<"\n sorry, insufficient balance";
        }
        else
        {
            cout<<"\nPlease take your cash";
            balance = balance - amount;
            cout<<"\n Your new balance is $"<<balance;
        }
    }
}

void saving ::show_balance()
{
    int acct_no;
    cout<<"\n show Saving account balance ";
    cout<<"\n Enter your account number :";
    cin>>acct_no;

    if(acct_no ==account_no)
        cout<<"\n Your balance is S "<<balance;
    else
        cout<<"\n Invalid account. Please try again";
}

void main()
{
    current cur;
    saving sav;
    char transaction_type;
    char act_type;
    cout<<"welcome to UniBank :";

    for(;;)
    {
        while(1)
        {
            cout<<"\nTraction type of availalble";
            cout<<"\nO - open account";
            cout<<"\nD - deposit";
            cout<<"\nW - withdraw";
            cout<<"\nB - Balance";
            cout<<"\nX - Exit";
            cout<<"\nEnter choice :";
            cin>>transaction_type;

           transaction_type = toupper(transaction_type);

            if(transaction_type = 'X')
                return;
            if(transaction_type = 'O' || 'D' || 'W' || 'B')
                break;
            else
                cout<<"\n Invalid type. Please try again";
        }

        while(1)
        {
            cout<<"\n\n Account types availalble";
            cout<<"\nC - Current";
            cout<<"\nS - Saving";
            cout<<"\nX - Exit";
            cout<<"Please enter account type :";
            cin>> act_type;
            act_type = toupper(act_type);

            if(act_type =='X')
                return;
            if(act_type =='C' || 'S')
                break;
            else
                cout<<"\n Invalid type.please try again";
        }

 switch(transaction_type)
        {
        case 'O' :
            if(act_type == 'C')
                cur.open_account();
            else
                sav.open_account();
            break;
         

        case 'D' :

            if(act_type == 'C')
                cur.deposit();
            else
                sav.deposit();
                break;
          
        case 'W' :

            if(act_type =='C')
                cur.withdraw();
            else
                sav.withdraw();
            break;

        case 'B' :
            if(act_type == 'C')
                cur.show_balance();
            else
            sav.show_balance();
            break;

        case 'X' : break;

        default :

            cout<<"\n Invalid type.";
            cout<<"Please try again";

        }
        cout<<endl;
    }
}

Recommended Answers

All 2 Replies

Hi mugilan :-)

The problem is in your if-statements, handling the menu:

if(transaction_type = 'X')
    return;
if(transaction_type = 'O' || 'D' || 'W' || 'B')
    break;
else
    cout<<"\n Invalid type. Please try again";

1) To compare transaction_type to 'X' you have to use the == (equality) operator, NOT the = (assignment) operator.

2) To compare transaction_type to several char-literals you write it like this:

if(transaction_type == 'O' || transaction_type == 'D' || transaction_type =='W' || transaction_type == 'B')

oops yea, Thank you very much friend, its solve my prob here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.