簡體   English   中英

獲取循環以重新顯示消息 C++

[英]Getting loop to redisplay message c++

我試圖讓一個程序來顯示一個帶有選項的菜單,然后給定用戶輸入,顯示某個消息。 在顯示他們的消息后,我希望循環返回顯示消息,直到他們選擇退出選項。 在選擇 1-3 后,如何讓循環返回顯示菜單?

#include <iostream>
#include <iomanip>
using namespace std;


int main(){
int menu_choice;
cout << "Select a numerical option:" << endl << "=== menu ===" << endl << "1. Fox" << endl << "2. Bunny" << endl << "3. Sloth" << endl << "4. Quit" << endl;
cin >> menu_choice;
while (menu_choice >= 1 && menu_choice <= 4)
{
    while (menu_choice == 1)
    {
        cout << "1 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 2)
    {
        cout << "2 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 3)
    {
        cout << "3 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 4)
    {
        cout << "4 work" << endl;
        menu_choice = 0;
        continue;
    }
}
    return 0;
}

您的代碼僅顯示菜單 1 次。 如果用戶輸入了無效的選項,您將立即退出。 如果用戶輸入了一個有效的選項,你就完成了選擇的工作,但隨后你退出而不是再次顯示菜單。 你所有的while..continue循環完全沒用,它們最多只運行 1 次,將menu_choice設置為 0,這會破壞你的外部while循環,因此它也只運行 1 次。

您需要一個連續運行的外循環,每次都顯示菜單,直到您真正准備好退出。

您還應該用if/else塊或更好的單個switch塊替換無用的while..continue循環。

嘗試更像這樣的事情:

#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int menu_choice;

    do
    {
        cout << "Select a numerical option:" << endl
             << "=== menu ===" << endl
             << "1. Fox" << endl
             << "2. Bunny" << endl
             << "3. Sloth" << endl
             << "4. Quit" << endl;

        if (!(cin >> menu_choice))
        {
            menu_choice = 0;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        switch (menu_choice)
        {
            case 1:
                cout << "1 work" << endl;
                break;

            case 2:
                cout << "2 work" << endl;
                break;

            case 3:
                cout << "3 work" << endl;
                break;

            case 4:
                cout << "4 work" << endl;
                break;

            default:
                cout << "Bad choice! Try again" << endl;
                break;
        }
    }
    while (menu_choice != 4);

    return 0;
}

我真的不認為你在這個問題上需要幫助,所以我有點不願意寫這個答案^^。

重復選擇通常以這種方式實現:

while (someCondition) {       // goes out if the condition isn't met
    cout << "The question" << endl;
    cin >> theAnswer;

    if (theAnswer == 1) {     // check the answer
        // do something
    }
                              // probably other checks
}

我建議你先在小程序中用if和while練習一下,比如問年齡,或者計算斐波那契列表

@Remy Lebeau 的發布速度更快,他的回答寫得很好,但由於我已經完成了代碼的編寫,您可能會受益於查看不同的實現,我發布了代碼。 我故意選擇不使用 switch,因為你之前可能沒有見過它。

#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    bool quit = false;
    int menu_choice;

    while(!quit) {
        cout << "Select a numerical option:" << endl
             << "=== menu ===" << endl
             << "1. Fox" << endl
             << "2. Bunny" << endl
             << "3. Sloth" << endl
             << "4. Quit" << endl;
        if(cin >> menu_choice) {
            if(menu_choice == 1) {
                cout << "1 work" << endl;
            }
            if(menu_choice == 2) {
                cout << "2 work" << endl;
            }
            if(menu_choice == 3) {
                cout << "3 work" << endl;
            }
            if(menu_choice == 4) {
                cout << "Bye" << endl;
                quit = true;  // Set quit to true to stop the while loop
            } else {
                cout << "Invalid number" << endl;
            }

        } else {
            cout << "Bad input" << endl;
            cin.clear();
            cin.ignore();
        }
    }
    return 0;
}

請注意, cin.ignorecin.clear很重要,並且經常使不cin.clear這一點的人cin.clear困惑。 閱讀這個問題了解詳情。

暫無
暫無

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

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