0

好的,所以我正在编写一个控制台应用程序,它使用开关来显示产品和价格。但是,当用户输入产品编号时,我正在弄清楚如何让它跳入开关

这是代码:

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');

}

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

4

2 回答 2

2

您的 switch 语句是“错误的”,因为您读取了一个整数然后将其与字符进行比较,这并不意味着同一件事。除非您真的想进入下一个案例,否则您还应该break;在每个案例的末尾都有一个。case

像这样的代码:

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

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

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

它在两个方面是错误的:首先answer = 'y'将变量设置answer'y'(并将语句变为“真”,因为整个表达式的值'y'与零比较(这将使其为假) - 它不是零,所以语句在此处结束并执行 if 中的内容。您应该使用“等于”运算符==,而不是“赋值运算符”=来检查某些内容是否匹配。如果您解决了这个问题,并且answer == 'y'不是'true,那么第二部分|| 'Y'不会'根本不做你想做的事,而是检查是否'Y'为零(事实并非如此)。

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

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

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

于 2013-04-17T09:28:29.933 回答
1

这是因为您将输入读取为整数,但案例是字符。

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

将大小写改为整数:

case 1:
    // ...

case 2:
    // ...

等等

于 2013-04-17T09:19:41.090 回答