Smokeyflo
Smokeyflo

Reputation: 31

Coin machine regarding decimals

Everything works as intended except one minor screwy line of code. The very last line that calculates the fee. For example if you type in 105 it says you entered $1.05, which is good, then it calculates the fee for the transaction which gives you $0.93555 as your take-home pay. I only want it to display up to the hundredths place no matter the dollar amount, not the hundred thousandth place. So it should display $0.93 because that's realistic. Note that depending on the integer you enter at the start, sometimes the decimal is placed correctly and sometimes the thousandth place is displayed, it's being screwy like that an I am not sure what to fix.

#include <iostream>
using namespace std;

int main() {
    int cents;
    double total;


    cout<<"Enter total amount of coins (whole number): "; //Enter any whole number
    cin>>total;
    cents = total;


    cout<<"You entered " << cents / 25 << " quarters";
    cents = cents % 25;

    cout<<", " << cents / 10 << " dimes";
    cents = cents % 10;

    cout<<", " << cents / 5 << " nickels";
    cents = cents % 5;

    cout<<", " << cents / 1 <<" pennies.";
    cents = cents % 1;

   cout<<" That is " << "$" <<total / 100 << "."<<endl; //Converting to dollar amount

   cout<<"After the fee, you take home " << "$" << (total - (0.109 * total)) / 100 << "."; //What you're left with after the fee

Upvotes: 1

Views: 130

Answers (3)

hamster on wheels
hamster on wheels

Reputation: 2893

The bug:

  • The take home fee does not give only two decimals places. Example: Entering 105 coins give a take home fee of 0.93555.

Expected behavior:

  • The take home fee should be rounded down to two decimal place. (I assume "The Company" want more money. So they want round down the take home money. Every penny counts.)

Tracing the possible causes of the bug:

  1. The take home fee is printed by the last line. So the last line may cause the bug.

  2. The last line's formula ((total - (0.109 * total)) / 100) depends on the variable total.

  3. Only the cin line (cin >> total;) and the definition of total (double total;) affects the variabletotal

These are all of the possible causes of the bug.

Simplified program that contains all possible causes of the bug:

#include <iostream>
using namespace std;

int main() {
    double total;

    // Enter any whole number
    cout << "Enter total amount of coins (whole number): ";
    cin >> total;

    // What you're left with after the fee
    cout << "After the fee, you take home " << "$"
         << (total - (0.109 * total)) / 100
         << ".";
}

Some ideas:

  • Idea 1: To round down a float number to integer, just cast it to integer. (e.g. cout << int(30.10) << "\n";)

  • Idea 2: Round down a float number to two decimal places is multiply the float number by 100, round down the result to integer, and then divide by 100.0.

  • If that sounds too tedious, can also use a library. see Rounding to 2 decimal points

A solution:

#include <iostream>
using namespace std;

int main() {
    // Enter any whole number
    cout << "Enter total amount of coins (whole number): ";
    double total;
    cin >> total;

    // calculate the take home fee without rounding
    double take_home_money = (total - 0.109 * total) / 100;
    // Round down the take home fee to two decimal places
    take_home_money = int(take_home_money * 100) / 100.0;

    // What you're left with after the fee
    cout << "After the fee, you take home $"
         << take_home_money << ".";
}

Also see the answer at C++ , A code to get an amount of money to convert into quarters, dimes , nickels, pennies

Upvotes: 0

Laszlo
Laszlo

Reputation: 801

In the last statement, the expression

(total - (0.109 * total)) / 100 

should be:

(total - int(0.109 * total))/100

(In this case you can get away with not using <iomanip> or any other extras. Simply cast the product to int)

Upvotes: 1

Nathan Wiley
Nathan Wiley

Reputation: 111

You can use setprecision() if you include <iomanip> in the header and then use fixed to set how ever many digits you want to display after the decimal.

These pages explain it pretty well:

http://www.cplusplus.com/reference/ios/fixed/

http://www.cplusplus.com/reference/iomanip/setprecision/

Upvotes: 2

Related Questions