I am very new to the concept of programming in C++. I am wanting to have a multi condition if statement using the ||
(or) and the &&
(and) in one statement. When I ask my college professor about it. She told it was possible and then insulted my limited knowledge on the subject. All examples I have access to show a multi && statement and only one showing the ||
. It does not show them being used together. I would like to learn how to get the line working. I will attach the code I have. The problem area is the last bit of coding.
# 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;
}
Your problem is that &&
has higher precedence than ||
so you need parens. As noted in a comment you also need to use ==
instead of assignment (=
):
if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)
Others have already helped you with the problem you've noticed. I'll start with a separate problem you apparently haven't noticed (yet):
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
If you want all three of those statements controlled by the else
, you need to enclose them in braces to create a compound statement:
else {
overTime = packagA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
}
As it stands right now, your code is really:
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
I.e., the computations for excessCharged
and amountDue
are carried out regardless of whether the condition in the if
statement was true or false.
I'd also note that your switch
statement doesn't really accomplish much:
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: ";
In particular, you take exactly the same action for all the cases (except the default). You can simplify this a bit by using fall-through cases:
switch (customerPackage) {
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
default:
cout << "Error " /* ... */;
}
Alternatively, you might consider something like:
static const char valid[] = "aAbBcC";
if (strchr(valid, userPackage)) {
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
}
else {
std::cout << "Error: Please enter the customer's purchased package";
std::cin >> userPackage;
}
Personally, however, I'd structure things a bit differently: first get one valid input, then get the next:
do {
std::cout << "Please enter the customer's purchased package (a, b, or c): ";
std::cin >> userPackage;
} while (!strchr(valid, userPackage));
std::cout << "Please enter the number of hours used: ";
std::cin >> hoursUsed;
if (tolower(customerPackage == 'a') && hoursUsed >= 10)
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With