0

So i am almost done with my program but i keep getting one penny off. For example when I input $6.01 as cost of item and $10.00 as the amount tendered I get the result as $3.99 but the denomination comes in as 3 one dollar bills, 3 quarters, 2 dimes, and 3 pennies ( which is supposed to be 4). How can I fix this?

static int assignment_change() {

//Variables

float cost_of_item, amount_tendered, total;
int change, pennies, nickels, dimes, quarters, dollar, five_dollars;

//Input

std::cout << "What was the cost of the item ($0.00 - $20.00)? $";
std::cin >> cost_of_item;
std::cout << "What was the amount tendered (Max $20.00)? $";
std::cin >> amount_tendered;


if (cost_of_item <= 20 && cost_of_item >= 0 && amount_tendered <= 20 && amount_tendered >= 0 && amount_tendered > cost_of_item)
{

    //Formulas

    total = amount_tendered - cost_of_item;
    change = (int)total * 100;
    five_dollars = change / 500;
    change %= 500;
    dollar = change / 100;
    change %= 100;
    quarters = change / 25;
    change = change % 25;
    dimes = change / 10;
    change %= 10;
    nickels = change / 5;
    change %= 5;
    pennies = change / 1;
    change %= 1;

    //Output

//-----------------------------------------------------------

    if (five_dollars >= 1)

        std::cout << five_dollars << " five dollar bills" << std::endl;

    else if (five_dollars == 0)

        std::cout << "";

//-----------------------------------------------------------

    if (dollar > 1)

        std::cout << dollar << " one dollars bills" << std::endl;

    else if (dollar == 0)

        std::cout << "";

    else if (dollar == 1)

        std::cout << dollar << " one dollar bill" << std::endl;

//-----------------------------------------------------------

    if (quarters > 1)

        std::cout << quarters << " quarters" << std::endl;

    else if (quarters == 0)

        std::cout << "";

    else if (quarters == 1)

        std::cout << quarters << " quarter" << std::endl;

//-----------------------------------------------------------

    if (dimes > 1)

        std::cout << dimes << " dimes" << std::endl;

    else if (dimes == 0)

        std::cout << "";

    else if (dimes == 1)

        std::cout << dimes << " dime" << std::endl;

//-----------------------------------------------------------

    if (nickels > 1)

        std::cout << nickels << " nickels" << std::endl;

    else if (nickels == 0)

        std::cout << "";

    else if (nickels == 1)

        std::cout << nickels << " nickel" << std::endl;

//-----------------------------------------------------------

    if (pennies > 1)

        std::cout << pennies << " pennies" << std::endl;

    else if (pennies == 0)

        std::cout << "";

    else if (pennies == 1)

        std::cout << pennies << " penny" << std::endl;

//-----------------------------------------------------------

    //std::cout << fixed << setprecision(2) << "Change from $" << amount_tendered << " for a cost of $" << cost_of_item << " is" << std::endl;
    std::cout << "Total is " << total << std::endl;


}

else

    std::cout << "*****Your input for the cost of the item or the amount that was to be tendered was outside the designated range.*****" << std::endl;

return 0;

}

  • 3
    Most likely because [the `float`ing point math you're using is broken](http://stackoverflow.com/questions/588004/is-floating-point-math-broken). Besides, this is way too over-engineered. The correct answer should require maybe a quarter of this code, at most. This would include dealing with the `float` issue. – Sam Varshavchik Oct 03 '16 at 00:38
  • 3
    Typical solutions are to multiply everything by 100 and operate in integers and pennies. Yes pennies, you stupid spell checker. Why on earth would I want penises? – user4581301 Oct 03 '16 at 00:43
  • 1
    @user4581301 ( ͡° ͜ʖ ͡°) – erip Oct 03 '16 at 00:58
  • @user4581301 half the population does... – M.M Oct 03 '16 at 02:42

1 Answers1

0

As mentioned in another comment, it is much simpler to change your total to an int, total = amount_tendered*100 - cost_of_item*100;, and work from there.

Ophi
  • 48
  • 7