How to exit a program after 3 trials to introduce a password in a bank operation?

674 views Asked by At

In the following code, when I try more than 3 times the password, the program doesn't exit, but continues to the next operation.

How can I make it exit, keeping the while(true) loop that I need for the rest of the program, so that when 'I wish to continue the operations' it starts the program again, asking the code again?:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {


    vector <string> options ={"1. Withdrawls", "2. Payments", "3. Transfers", 
    "4. Consultations"};    
    vector <string> money = {"1. 20€", "2. 40€", "3. 60€", "4. 80€", "5. 100€", 
    "6. 150€", "7. Other amount"};
    vector <string> consultations = {"1. Bank consultation", "2. Movements        
    consultation", "3. BAN consultation"};
    int i, o, p, j, cod, amount, entity, reference, BAN;
    int pass= 2334, counter=1;

    while (true){

        cout << "Introduce your password." << endl;
        cin >> cod;     

        while(pass!=cod && counter <4){

            cout << "Wrong code. You have three trials" << endl;
            cin >> cod;                 
            cout << counter << endl;
            counter++;
        }       

        string recipt, operations;      
        cout << "Choose one of the following options:"<< endl;

        for (i =0; i<4; i++)
            cout << options[i] << endl; 

        cin >> o;       
        cout << options[o-1] << endl;

        if (o==1){

            for (j=0; j<7;j++)
                cout << money[j] << endl;       

            cin >> j;               
            if (j==7){

                cout << "Amount?";
                cin >> amount;
            }
            else 
                cout << money[j-1] << endl << endl;

            cout << "Do you wish a recipt? YES or NO?"<< endl;
            cin >> recipt;  

            if (recipt == "YES"){
                cout << "Withdraw the recipt" << endl
                << "Withdraw the money" << endl;                
            }       

            else if (recipt == "NO")
                cout << "Withdraw the money" << endl;   
            cout << "Do you wish to do other operations? YES or NO?" << endl;   
            cin >> operations; 

            if (operations== "NO"){
                cout << "Withdraw the card";    
                break;
            }
        }

        else if (o==2){

            cout << " ENTITY " << endl;
            cin >> entity; 
            cout << " RFERENCE " << endl;
            cin >> reference; 
            cout << " AMOUNT" << endl;
            cin >> amount; 
            cout << endl << "Withdraw the ticket" << endl;
            cout << "Do you wish to do other operations? YES or NO?" << endl;   
            cin >> operations; 
            if (operations== "NO"){
                cout << "Withdraw the card";    
                break;
            }       
        }

        else if (o==3){

            cout << " Payee BAN?" << endl;
            cin >> BAN;
            cout << " Amount?" << endl;
            cin >> amount;
            cout << endl << "Withdraw the ticket" << endl << endl;
            cout << "Do you wish to do other operations? YES or NO?" << endl;   
            cin >> operations; 

            if (operations== "NO"){
                cout << "Withdraw the card";    
                break;
            }                   
        }

        else if (o==4){

            for (p=0; p<3;p++){

                cout << consultations[p] << endl;
            }
            cin >> p; 
            cout << endl << consultations[p-1] << endl
            << "Withdraw the ticket" << endl << endl;
            cout << "Do you wish to do other operations? YES or NO?" << endl;   
            cin >> operations; 

            if (operations== "NO"){
                cout << "Withdraw the card";    
                break;
            }                   
        }
    }
    return 0;
}
1

There are 1 answers

0
aakansha On

I guess you want to break the outer while loop when inner while fails. You can break after checking whether pass==cod after completion of inner while.

while (true){
    cout << "Introduce your password." << endl;
    cin >> cod;     
    while(pass!=cod && counter <4){
        cout << "Wrong code. You have three trials" << endl;
        cin >> cod;                 
        cout << counter << endl;
        counter++;
    }    
    if(pass!=cod)//break the loop if there is no match
         break;
}

Hope this solves the problem.