0
#include<iostream.h>


int main()
{
    cout<<"*************Welcome************\n\n";

    int pass;
    int option;
    int balance=50000;
    int withdrawal;
    int deposit;
    cout<<"Please Enter Your Password:";
    for (int i=0;i<3;i++)
    {

            cin>>pass; 
            cout<<"\n\n";
            if(pass==2468)
            {

                cout<<"**Choose an Option**\n\n";
                cout<<"1. Balance Inquiry\n";
                cout<<"2. Withdrawal\n";
                cout<<"3. Deposit\n";
                cout<<"4. Mobile Refill\n";
                cout<<"5. Exit\n";
                cout<<"Enter Option:";
                cin>>option;

                switch(option)
                {
                    case 1:
                        cout<<"\t **Balance Inquiry**\n";
                        cout<<"Your Current Balance is:"<<balance<<endl;
                        break;

                    case 2:
                        cout<<"\t **Withdrawl**\n";
                        cout<<"Enter amount:";
                        cin>>withdrawal;

                        balance=balance-withdrawal;
                        cout<<"Your remaining balance is $"<<balance<<endl;
                    continue;

                    case 3:
                        cout<<"\t **Deposit**\n"; 
                        cout<<"Enter amount: $";
                        cin>>deposit;

                        balance = balance + deposit;
                        cout<<"Your new balance is $"<<balance<<endl;
                    continue;

                    case 4:
                        cout<<"\t **Mobile Refill**\n";
                        cout<<"\t **Deposit**\n";
                    break;

                    default:
                        cout<<"This is an invalid option. \n";

                }
            }
            else
            {

                cout<<"Incorrect Password,Please Try Again :";

            }



    }
    cout<<"You Entered The Wrong Password More Than 3 Times!.Good Bye";
}

I am unable to get out of the loop,.eg if i choose i get the right password and enter the loop, following an option, i cannot choose another option.Please help

3
Contributors
3
Replies
7
Views
5 Years
Discussion Span
Last Post by ougesh
0

Start by declaring a boolean variable before the for-loop:

bool correct_pass = false;

Then, you set it to true inside the for-loop, when the password is correctly entered:

for (int i=0;i<3;i++)
{
    ...
    if (pass=2468)
    {
        correct_pass = true;
        break;
    }
    ...
}

Then you just check to see if the bool is true outside the for-loop (the break statement gets you out of the for-loop). You can then use another loop for the options menu.

0

As nmaillet implies, your loop should be only around your password test, not the entire program.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.