Question: So I'm having this issue with step functions and C++ where no matter what I do the step function always reverts to the upper even

So I'm having this issue with step functions and C++ where no matter what I do the step function always reverts to the upper even when I have it specified in the lower with <=. A friend said it has to do with the floating-point but I have no idea how to fix it. If anyone can help me thank you. The issue is with cent values on the cusp not being evaluated properly so the centsTax is incorrect. It is in the upper cusp instead of the lower cusp it should be in.

Updated

#include

#include

using namespace std;

int main() {

int units;

float unitCost, cents, dollarTax, totalAmountDueBeforeTax, totalAmountDue, centsTax = 0;

cout << setw(3) << endl;

cout << "Please enter the cost of each candy bar: ";

cin >> unitCost;

cout << "Please enter the quantity: ";

cin >> units;

totalAmountDueBeforeTax = units * unitCost;

int dollars = totalAmountDueBeforeTax;

cout << "dollars " << dollars << endl;

dollarTax = dollars * .05;

cout << "dollartax " << dollarTax << endl;

cents = totalAmountDueBeforeTax - dollars;

cout << setprecision(2) << cents << endl;

cout << "Cents " << cents << endl;

if (cents >= .01 && cents <= 0.20) {

centsTax = .01;

}

else if (cents > .20 && cents <= 0.40) {

centsTax = .02;

}

else if (cents > .40 && cents <= 0.60) {

centsTax = .03;

}

else if (cents > .60 && cents <= 0.80) {

centsTax = .04;

}

else if (cents > .80 && cents <= 1.0) {

centsTax = .05;

}

else {

}

cout << "centsTax " << centsTax << endl;

totalAmountDue = dollars + cents + dollarTax + centsTax;

cout << setprecision(3) << "Your total amount due is $" << totalAmountDue << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!