我对 C++ 编程的概念非常陌生。我想要使用多条件 if 语句||
(或)和&&
(和)在一份声明中。当我向我的大学教授询问此事时。她告诉我这是可能的,然后侮辱了我在这个问题上有限的知识。我有权访问的所有示例都显示了多个 && 语句,并且只有一个显示了||
。它没有显示它们被一起使用。我想了解如何让生产线正常工作。我将附上我的代码。问题区域是编码的最后一位。
# include <iostream>
# include <cstring>
using namespace std;
main()
{
const int maximumHours = 774;
char customerPackage;
double hoursUsed = 0,
packageA = 9.95,
packageB = 14.95,
packageC = 19.95,
overPackageA = 2.00,
overPackageB = 1.00,
overTime = 0,
amountDue = 0,
excessCharged = 0;
cout << "Please enter the customer's package: ";
cin >> customerPackage;
switch (customerPackage)
{
case 'a' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'A' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'b' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'B' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'c' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'C' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
default: cout << "Error."
<< " Please enter the customer's purchased package: ";
cin >> customerPackage;
}
if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)
amountDue = packageA;
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
}
你的问题是&&
优先级高于||
所以你需要括号。正如评论中所述,您还需要使用==
而不是赋值(=
):
if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)