Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions