簡體   English   中英

無法弄清楚如何為用戶提供啟動、停止和重新啟動程序的所有選項?

[英]Can't figure out how to give user all the options for starting, stopping, and restarting program?

好的,所以我正在嘗試構建這個隨機數出納機,它基本上告訴用戶他們輸入的數字是否小於、大於或等於 50,並為他們提供啟動、停止和重新啟動“隨機數出納機”的選項"這是代碼:

    #include <iostream>


using namespace std;

main() {
        cin >> boolalpha;

        int invalid_answer {0};
        const int const_num {50};
        int random_num {};
        char answer {};
        int keep_going {};

        while (keep_going == 0) {
            while (invalid_answer == 0) {

    //=======================================================================================================================================

                cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
                cin >> random_num;

                if (random_num > const_num) {

                    cout << random_num << " is greater than " << const_num;
                }
                else if (random_num == const_num) {

                    cout << random_num << " is the same as " << const_num << endl;
                }
                else {

                    cout << random_num << " is less than " << const_num << endl;
                }

                cout << "Want to try again? Type \"Y\" or \"N\"";
                cin >> answer;

    //=======================================================================================================================================

                if (answer == 'N') {

                    cout << "Ok then, sorry to see you miss out" << endl;
                    keep_going = 1;
                    }

    //=======================================================================================================================================

                while(answer == 'Y') {

                    cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
                    cin >> random_num;

                    if (random_num > const_num) {

                        cout << random_num << " is greater than " << const_num;
                    }
                    else if (random_num == const_num) {

                        cout << random_num << " is the same as " << const_num << endl;
                    }
                    else {

                        cout << random_num << " is less than " << const_num << endl;
                    }

                    cout << "\nWant to try again? Type \"Y\" or \"N\"";
                    cin >> answer;
                }

    //=======================================================================================================================================

                if (answer != 'Y' || answer != 'N') {
                    invalid_answer = 1;
                    }

    //=======================================================================================================================================

                while (invalid_answer == 1) {
                    cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
                    cin >> answer;

                    if (answer == 'Y') {
                        invalid_answer = 0;
                        }
                    else if (answer == 'N') {
                    cout << "Ok then, sorry to see you miss out" << endl;
                    keep_going = 1;
                    }
            }
            }        
        }

}

每當我說“N”表示“否”時,我不想重做隨機數檢查器,它不會將 keep_going 更改為 1,它只是移動到它下面的其他 if 或 while 語句之一。 因此,當您輸入“N”時,它只會輸出"Enter a random number and we will tell you if it is greater than or less than " << const_num << ": ""I'm sorry what? Please note that answers are case sensitive. Answer again: "

問題在於這段代碼:

   if (answer != 'Y' || answer != 'N') {
       invalid_answer = 1;
   }

answer'N'answer != 'Y'trueinvalid_answer設置為 1(由於短路評估,邏輯 OR 的rhs甚至不被評估 - 請參閱下面的引用)。
所以執行會進入while

while (invalid_answer == 1)

並將打印報表。

您可以通過以下方式更正:

if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
           invalid_answer = 0;
}
else { //for all other inputs
           invalid_answer = 1;
}

內置運算符&&|| 執行短路評估如果在評估第一個操作數后結果已知,則不評估第二個操作數),但重載運算符的行為類似於常規函數調用並且始終評估兩個操作數

另請注意, main應具有int類型。

我在發布問題后馬上就想通了哈哈,基本上上面的答案是正確的,所以我不得不將該 if 語句拆分為另外 2 個,其中我在每個語句中添加了一個 else 語句,表示invalid_answer = 0; 確保;確定。 但是在用戶第二次使用該程序后,如果他們想退出它不會讓他們重新啟動它。 我通過添加解決了這個問題

if (answer == 'N') {

                    cout << "Ok then, sorry to see you miss out" << endl;
                    keep_going = 1;
                    }`

while(answer == 'Y')循環的底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM