c++不知道如何从用户输入跳转到开关

C++ cannot figure out how to jump into the switch from user input

本文关键字:开关 输入 用户 不知道 c++      更新时间:2023-10-16

我正在编写一个控制台应用程序,它使用一个开关来显示产品和价格。然而,当用户输入产品编号

时,我很难弄清楚如何让它跳转到开关中

这是代码:

using namespace std;
int main()
{
int productNum = 0;  
int quantityArray[5];
for (int i = 0; i < 5; ++i)
quantityArray[i] = 0;
char answer = ('y' || 'n' || 'e' || 'Y' || 'N' || 'E');
double priceArray[5] = { 2.98, 4.50, 9.98, 4.99, 6.87 };
double total1 = (quantityArray[0] * priceArray[0]);
double total2 = (quantityArray[1] * priceArray[1]);
double total3 = (quantityArray[2] * priceArray[2]);
double total4 = (quantityArray[3] * priceArray[3]);
double total5 = (quantityArray[4] * priceArray[4]);
double checkout = total1 + total2 + total3 + total4 + total5;
cout << "Please select a product number 1, 2, 3, 4, or 5" << endl;
cin >> productNum;
do
    {
       switch (productNum)
       {
       case '1':
           cout << "1 inch x 1 inch sticker is $" << priceArray[0] << endl;
       cout << "How many of these stickers would you like to purchase?" << endl;
        cin >> quantityArray[0];
              cout << "You have selected " << quantityArray[0] << "at the price of $" << priceArray[0] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '2':
              cout << "3 inch x 2 inch sticker is $" << priceArray[1] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[1];
              cout << "You have selected " << quantityArray[1] << "at the price of $" << priceArray[1] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '3':
              cout << "7 inch x 7 inch sticker is $" << priceArray[2] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[2];
              cout << "You have selected " << quantityArray[2] << "at the price of $" << priceArray[2] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '4':
              cout << "3 inch x 3 inch sticker is our current special at $" << priceArray[3] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[3];
              cout << "You have selected " << quantityArray[3] << "at the price of $" << priceArray[3] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
       case '5':
              cout << "5 inch x 4 inch sticker is $" << priceArray[4] << endl;
              cout << "How many of these stickers would you like to purchase?" << endl;
              cin >> quantityArray[4];
              cout << "You have selected " << quantityArray[4] << "at the price of $" << priceArray[4] << endl;
              cout << "Is this correct?" << endl;
              cout << "Y for yes, N for no, E to checkout." << endl;
              cin >> answer;
                if (answer = 'y' || 'Y')
                {
                    break;
                }
                else if (answer = 'n' || 'N')
                {
                    cout << "Please select a qantity." << endl;
                }
                else if (answer = 'e' || 'E')
                {
                    cout << checkout << endl;
                }
       }
    }while (answer != 'e' || 'E');

}

我的书使用头文件来显示如何初始化开关,但它使用多个方法。我很确定我可以在不使用标题的情况下将其纳入一个方法。

你的switch语句是"错误的",因为你读取一个整数,然后将其与字符进行比较,这并不意味着同样的事情。你还应该在每个case的末尾有一个break;,除非你真的想要进入下一个情况。

代码如下:

 (answer = 'y' || 'Y')

可以重写为这样(为了澄清它的作用):

 ((answer = 'y') || 'Y')

它在两个方面是错误的:首先answer = 'y'将变量answer设置为'y'(并且它将语句变为'true',因为整个表达式的值是'y',与零相比(这将使其为假)-它不是零,因此语句结束并执行if内部的内容。您应该使用"等于"操作符==,而不是"赋值操作符"=来检查是否匹配。如果您修复了这个问题,并且answer == 'y'不为真,则第二部分|| 'Y'根本不会做您想要的,而是检查'Y'是否为零(并且它不是)。

要解决这个问题,您需要使用if (answer == 'y' || answer == 'Y')和类似的

同样适用于这个char answer = ('y' || 'n' || 'e' || 'Y' || 'N' || 'E');,它将答案设置为true[或1],因为值的逻辑值不为零-并且只有'y'被检查,因为C和c++定义为"一旦我们找到决定整个序列的东西就停止检查"。

我有两个建议:1. 每次只写一点点代码。在继续下一步之前,测试它是否有效。}2. 在编译器中启用警告。经常编译。这将帮助您在编写大量"错误"代码之前检测并修复问题。

这是因为您将输入读取为整数,但大小写是按字符的。

例如,字符 '1'(在绝大多数计算机上)与整数 49相同。

将大小写改为整数:

case 1:
    // ...
case 2:
    // ...

等。