簡體   English   中英

我正在嘗試在 c++ 中制作菜單,但我不明白為什么它不循環

[英]I'm trying to make a menu in c++ and I don't understand why it doesn't loop

這是我到目前為止所嘗試的。 可能是我使用了 wearg do while 語法,但我真的很難過,因為我很確定我之前使用過這個精確的模板來創建菜單。 我認為 while 循環中的條件由於某種原因是錯誤的,它忽略了該行並停止了程序。

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

int main()
{
    char selection{};
    do{
        cout << "P: Print the numbers in the vector" << endl;
        cout << "A: Add a number to the vector" << endl;
        cout << "M: Display the mean of the vector" << endl;
        cout << "S: Display the smallest number in the vector" << endl;
        cout << "L: Display the largest number in the vector" << endl;
        cout << "Q: Quit" << endl;
        cout << "Enter your selection: ";
        cin >> selection;

        if (selection == 'P' || selection == 'p'){
            cout << "You chose to print" << endl;
        }
        else if (selection == 'A' || selection == 'a'){
            cout << "You chose to add a number to the vector" << endl;
        }
        else if (selection == 'M' || selection == 'm'){
            cout << "You chose to calculate the mean" << endl;
        }
        else if (selection == 'S' || selection == 's'){
            cout << "You chose to display the smallest number" << endl;
        }
        else if (selection == 'L' || selection == 'l'){
            cout << "You chose to display the largest number" << endl;
        } 
        else if (selection == 'Q' || selection == 'q'){
            cout << "Thank you for using the program" << endl;
        }
        else {
            cout << "Invalid selection try again" << endl;

        }
    }
    while (selection == 'Q' && selection == 'q');

    return 0;
}

正如@Yksisarvinen 寫的那樣,您的狀況有問題,您當前的狀況表明:如果選擇等於 Q並且等於 q 運行循環,我猜您不想要什么,所以您需要更改循環條件由此 -

  while (selection == 'Q' && selection == 'q');

對此——

 while (selection != 'Q' && selection != 'q');

暫無
暫無

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

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