繁体   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