c++不知道如何从用户输入跳转到开关
C++ cannot figure out how to jump into the switch from user input
我正在编写一个控制台应用程序,它使用一个开关来显示产品和价格。然而,当用户输入产品编号
时,我很难弄清楚如何让它跳转到开关中这是代码:
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:
// ...
等。
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- C++:如果用户输入数字将其转换为单词,并且如果按回车键程序应退出,则使用开关大小写
- 将int输入函数,将int输入开关,然后更改int的值
- 输入正整数或负整数列表,以使用开关函数 c++ 确定数字是偶数还是奇数
- 开关大小写语句中函数中的字符串输入会导致无限循环和绕过输入
- 从用户输入中提取整数的枚举开关语句
- 如何为开关机箱提供可靠的用户输入
- C++ - 使用开关的用户输入和输出
- 如果我们在需要整数的开关情况下输入字符会发生什么
- getline() 不等待开关内部的输入.案例(C++)
- 无法弄清楚为什么我的开关不工作。似乎无法识别 cin 输入
- c++不知道如何从用户输入跳转到开关