C ++无法弄清楚如何从用户输入跳转到交换机

时间:2013-04-17 09:18:29

标签: c++ loops switch-statement do-while

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

时,我正在搞清楚如何让它跳入开关

这是代码:

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但它使用多种方法。我很确定我可以在不使用标题的情况下将其转换为一种方法。

2 个答案:

答案 0 :(得分:2)

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

这样的代码:

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

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

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

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

要解决此问题,您需要使用'Y'和类似的内容。

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

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

答案 1 :(得分:1)

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

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

将案例更改为整数:

case 1:
    // ...

case 2:
    // ...