首页 > 解决方案 > 即使满足条件,while循环也不起作用

问题描述

我有一个任务是为基本的 atm 功能编写一个 C++ 程序。有一条规则是,一天内最多可以有 50,000 笔现金流过自动取款机,包​​括所有现金转账、取款或快速现金。我在整个主体上放置了一个while循环,它使用“atmlimit”整数来测试条件。每笔成功的现金交易都会被添加到其中,但由于某种原因循环无法正常工作。

#include <iostream>
using namespace std;
int main()
{
    int pincode, pindigit1;
    int availbalance, userchoice;
    int transmoney, inputFC, ubinput;
    int cwmoney, ctmoney, spam;
    int atmlimit;

    cout << "\n           Please enter your 4-digit pin code \n           Enter 'break' at any point to close the process \n" << endl;
    cin >> pincode;

    pindigit1 = pincode;

    for (pindigit1; pindigit1 >= 10; pindigit1 /= 10);

    availbalance = 10000 * pindigit1;

    while (atmlimit <= 50000) {

    menu:
        cout << "\n        *******************************************************************************" << endl;
        cout << "\n        *   Please choose the service your require by entering its respective number  *" << endl;
        cout << "\n        *   [1] Fast Cash                                                             *" << endl;
        cout << "\n        *   [2] Cash Withdrawl                                                        *" << endl;
        cout << "\n        *   [3] Balance Inquiry                                                       *" << endl;
        cout << "\n        *   [4] Cash Transfer                                                         *" << endl;
        cout << "\n        *******************************************************************************  \n" << endl;
        cin >> userchoice;

        if (userchoice == 1) {

        ta:

            cout << "\n          Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl;
            cout << "\n          [1] 500Rs                     [2] 1000Rs" << endl;
            cout << "\n          [3] 2000Rs                    [4] 3000Rs" << endl;
            cout << "\n          [5] 4000Rs                    [6] 5000Rs" << endl;
            cout << "\n          [7] 10000Rs                   [8] 20000Rs \n" << endl;
            cin >> inputFC;

            if (inputFC == 8) {
                transmoney = 20000;
            }

            if (inputFC == 7) {
                transmoney = 10000;
            }

            if (inputFC == 6) {
                transmoney = 5000;
            }

            if (inputFC == 5) {
                transmoney = 4000;
            }

            if (inputFC == 4) {
                transmoney = 3000;
            }

            if (inputFC == 3) {
                transmoney = 2000;
            }

            if (inputFC == 2) {
                transmoney = 1000;
            }

            if (inputFC == 1) {
                transmoney = 500;
            }

            if (inputFC == 0) {
                goto menu;
            }

            if (transmoney <= availbalance) {
                cout << "\n                      Your transaction completed successfully" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += transmoney;
                availbalance -= transmoney;

                goto menu;
            }

            if (transmoney > availbalance) {
                cout << "\n                      You have insufficient balance to perform this transaction. Enter '0' to go back the menu or '1' to change the transaction amout" << endl;
                cin >> ubinput;
            }

            if (ubinput == 0) {
                goto menu;
            }
            if (ubinput == 1) {
                goto ta;
            }
        }

        if (userchoice == 2) {
        cwmenu:
            cout << "\n                      Please enter the amount of cash you want to withdraw from your bank account \n              Enter 0 if you want to return to the menu \n" << endl;
            cin >> cwmoney;

            cout << cwmoney; //TBR

            if (cwmoney == 0) {
                goto menu;
            }

            if (cwmoney <= availbalance) {
                cout << "\n                       Your transaction bas been processed, Thank you!\n" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += cwmoney;
                availbalance -= cwmoney;

                goto menu;
            }
            else if (cwmoney > availbalance) {
                cout << "\n                      You have insufficient balance for this transaction. \n" << endl;
                goto cwmenu;
            }
        }

        if (userchoice == 3) {
            cout << "\n                    Your current bank balance is " << availbalance << "Rs" << endl;
            cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
            goto menu;
        }

        if (userchoice == 4) {
        ctmenu:

            cout << "\n                      Enter the  receiver's 6 digit bank account number \n                       Enter 0 to return to the menu" << endl;
            cin >> spam;
            if (spam == 0) {
                goto menu;
            }

            cout << "\n                   Please enter the amount of cash you want to transfer/send \n                             Enter 0 if you want to return to the menu \n" << endl;
            cin >> ctmoney;

            if (ctmoney == 0) {
                goto menu;
            }

            cout << "\n" << endl;

            cout << ctmoney << endl; //TBD

            if (ctmoney <= availbalance) {
                cout << "\n                      Your transaction bas been processed, Thank you!\n" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += availbalance;
                availbalance -= ctmoney;

                goto menu;
            }
            else if (ctmoney > availbalance) {
                cout << "\n                       You have insufficient balance for this transaction. \n" << endl;
                goto ctmenu;
            }
        }
    }

    cout << "\n                    You have reached the maximum amount of cash flow through your account, Please try later \n" << endl;
}

在所有的建议之后,我重新编写了这样的代码:

#include <iostream>
using namespace std;
int main()
{
    int pincode, pindigit1;
    int availbalance, userchoice;
    int transmoney, inputFC;
    int cwmoney, ctmoney;
    int atmlimit = 0;

    cout << "\n           Please enter your 4-digit pin code \n           Enter 'break' at any point to close the process \n" << endl;
    cin >> pincode;

    pindigit1 = pincode;

    for (pindigit1 = pincode; pindigit1 >= 10; pindigit1 /= 10)
        ;
    cout << pindigit1 << endl; //TBR

    availbalance = 10000 * pindigit1;
    cout << availbalance << endl; //TBR

    while (atmlimit <= 50000) {

        cout << "\n        *******************************************************************************" << endl;
        cout << "\n        *   Please choose the service you require by entering its respective number  *" << endl;
        cout << "\n        *   [1] Fast Cash                                                             *" << endl;
        cout << "\n        *   [2] Cash Withdrawl                                                        *" << endl;
        cout << "\n        *   [3] Balance Inquiry                                                       *" << endl;
        cout << "\n        *   [4] Cash Transfer                                                         *" << endl;
        cout << "\n        *******************************************************************************  \n" << endl;
        cin >> userchoice;

        switch (userchoice) {

        case 1:

            cout << "\n          Choose the amount of money you want to send by entering its respective number or enter 0 to go back to menu" << endl;
            cout << "\n          [1] 500Rs                     [2] 1000Rs" << endl;
            cout << "\n          [3] 2000Rs                    [4] 3000Rs" << endl;
            cout << "\n          [5] 4000Rs                    [6] 5000Rs" << endl;
            cout << "\n          [7] 10000Rs                   [8] 20000Rs \n" << endl;
            cin >> inputFC;

            if (inputFC == 8) {
                transmoney = 20000;
            }

            if (inputFC == 7) {
                transmoney = 10000;
            }

            if (inputFC == 6) {
                transmoney = 5000;
            }

            if (inputFC == 5) {
                transmoney = 4000;
            }

            if (inputFC == 4) {
                transmoney = 3000;
            }

            if (inputFC == 3) {
                transmoney = 2000;
            }

            if (inputFC == 2) {
                transmoney = 1000;
            }

            if (inputFC == 1) {
                transmoney = 500;
            }

            if (inputFC == 0) {
                continue;
            }

            if (transmoney <= availbalance) {
                cout << "\n                      Your transaction completed successfully" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += transmoney;
                availbalance -= transmoney;

                cout << atmlimit << endl; //TBD
                cout << availbalance; //TBD

                continue;
            }

            if (transmoney > availbalance) {
                cout << "\n                      You have insufficient balance to perform this transaction" << endl;
                continue;
            }

            break;

        case 2:

            cout << "\n                      Please enter the amount of cash you want to withdraw from your bank account \n              Enter 0 if you want to return to the menu \n" << endl;
            cin >> cwmoney;

            cout << cwmoney; //TBR

            if (cwmoney == 0) {
                continue;
            }

            if (cwmoney <= availbalance) {
                cout << "\n                       Your transaction bas been processed, Thank you!\n" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += cwmoney;
                availbalance -= cwmoney;

                cout << atmlimit << endl;
                cout << availbalance;

                continue;
            }
            else if (cwmoney > availbalance) {
                cout << "\n                      You have insufficient balance for this transaction. \n" << endl;
                continue;
            }
            break;

        case 3:
            cout << "\n                    Your current bank balance is idk" << availbalance << "Rs" << endl;
            cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
            break;

        case 4:

            cout << "\n                   Please enter the amount of cash you want to transfer/send \n                             Enter 0 if you want to return to the menu \n" << endl;
            cin >> ctmoney;

            if (ctmoney == 0) {
                continue;
            }

            cout << "\n" << endl;

            cout << ctmoney << endl; //TBD

            if (ctmoney <= availbalance) {
                cout << "\n                      Your transaction bas been processed, Thank you!\n" << endl;
                cout << "\n ------------------------------------------------------------------------------------------------------------------------------------------- \n" << endl;
                atmlimit += ctmoney;
                availbalance -= ctmoney;

                cout << atmlimit << endl; //TBD
                cout << availbalance;

                continue;
            }
            else if (ctmoney > availbalance) {
                cout << "\n                       You have insufficient balance for this transaction. \n" << endl;
                continue;
            }
            break;
        }
    }
    cout << "\n                    You have reached the maximum amount of cash flow through your account, Please try later \n" << endl;
}

标签: c++while-loop

解决方案


通过将 goto 语句替换为 break/continue 以及评论中提到的其他一些小调整来解决此问题。


推荐阅读