Logic error
Feb 24, 2021 at 7:51pm UTC
Wondering why my math isn't coming out correctly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <iomanip>
using namespace std;
double getStandard(double total);
double getPremium(double total);
int main()
{
//variable
double userTotal;
//ask for user input
cout << fixed << setprecision(2) << endl;
cout << "Please enter your total before shipping ($0.00): " ;
cin >> userTotal;
//get totals for each membership
cout << "Standard membership price: " << getStandard(userTotal) << endl;
cout << "Premium membership price: " << getPremium(userTotal) << endl;
}
//return methods to calculate total with standard and premium shipping
double getStandard(double total)
{
double standardTotal;
if (total >= 0 || total <= 100)
{
standardTotal = total + 12.99;
}
else
{
standardTotal = total + 4.99;
}
return standardTotal;
}
double getPremium(double total)
{
double premiumTotal;
if (total >= 0 || total <= 49.99)
{
premiumTotal = total + 4.99;
}
else
{
premiumTotal = total;
}
return premiumTotal;
}
Last edited on Feb 24, 2021 at 7:52pm UTC
Feb 24, 2021 at 8:40pm UTC
You presumably want to use && instead of ||. With || the 'if' part will always be done if total is >= 0 since if EITHER of the conditions is true the 'if' block is run. You only want it to run if BOTH the conditions are true.
And the second part of the getPremium condition should be total < 50
.
Feb 24, 2021 at 8:46pm UTC
Thank you.
Topic archived. No new replies allowed.