【问题标题】:How to exit a program after 3 trials to introduce a password in a bank operation?3次尝试后如何退出程序以在银行操作中引入密码?
【发布时间】:2015-09-04 07:39:59
【问题描述】:

在下面的代码中,当我尝试超过3次密码时,程序并没有退出,而是继续下一个操作。

如何让它退出,保留程序其余部分所需的 while(true) 循环,以便在“我希望继续操作”时再次启动程序,再次询问代码?:

#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;
}

【问题讨论】:

  • 所以如果用户输入错误密码超过 3 次,您希望进程重新启动[从while true 正文开始]?
  • 不,我希望当用户输入密码超过3次时,程序结束,并要求我取卡,就像我不想再操作时一样程序的其余部分。
  • 您想暂停程序并在用户选择时重新启动它吗?
  • 当用户在 3 次尝试后没有设置正确的密码时,以及当他被问到:“你想进行其他操作吗?”时,我想停止程序。 .当用户被问到“你想进行其他操作吗?”时,我想在用户说“是”时重新启动程序
  • 你的问题很不清楚,有很多条件流程,你假设了很多我们不知道的事情!

标签: c++ while-loop break


【解决方案1】:

我猜你想在内部 while 失败时打破外部 while 循环。 内部while完成后检查pass==cod是否可以break。

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;
}

希望这能解决问题。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2021-11-21
    • 2018-04-22
    • 1970-01-01
    相关资源
    最近更新 更多