mercredi 22 juin 2016

C++ Double to Int Conversion Complication


I'm trying to create a cash register application in C++ which asks for the price of an item and the payment amount from the customer, where it then displays the change in dollars, quarters, dimes, and pennies:

#include <iostream>
using namespace std;

void printChange(int&, int&, int&, int&, int&);
void findCoins(int&, int&, int&, int&, int&);


int main()
{
    double price;
    double payment;
    char answer = 'y';
    int dollars, quarters, dimes, nickels, pennies;
    while(answer == 'y')
    {
        cout<<"Enter price of an item: "<<endl;
        cin>>price;
        cout<<"Enter payment from customer: "<<endl;
        cin>>payment;
        double change = payment - price;
        dollars = change;               //use implicit conversion
        change = change * 100;          //multiplication

        int coins = change - dollars * 100;
        findCoins(coins, quarters, dimes, nickels, pennies);

        printChange(dollars, quarters, dimes, nickels, pennies);
        cout<<"Do you have another transaction?";
        cin>>answer;
    }
    cout<<"Thanks for shopping at Albertsons!"<<endl;
    return 0;
}

void printChange(int& dol, int& q, int& d, int& n, int& p)
{
    cout<<"dollars "<<dol<<endl;
    cout<<"quarters: "<<q<<endl;
    cout<<"dimes:  "<<d<<endl;
    cout<<"nickels: "<<n<<endl;
    cout<<"pennies: "<<p<<endl;
}
void findCoins(int& coins, int& quarters, int& dimes,
        int& nickels, int& pennies)
{
    quarters = coins/25;                //use implicit conversion
    dimes = coins % 25 / 10;            //use remainder division
    nickels = coins % 25 % 10 / 5;
    pennies = coins % 25 % 10 % 5;
}

The problem here is that I need to convert change (which is a double) into coins (which is an integer) so that I can use modulus in determining how many quarters, dimes, nickels, and pennies the cashier owes the customer and no matter what angle I approach the problem I keep getting the same error - the original double value is subtracted by 1 (in some cases).

E.g.

Enter price of an item: 
10
Enter payment from customer: 
10.69
dollars 0
quarters: 2
dimes:  1
nickels: 1
pennies: 3

Compared to:

Enter price of an item: 
1.26
Enter payment from customer: 
5.00
dollars 3
quarters: 2
dimes:  2
nickels: 0
pennies: 4

NOTE: I understand that I can utilize fmod() instead of modulus for double values, but I still ran into the same type of truncation error even when changing coins into a double, specifically in the following scenario:

Enter price of an item: 
45
Enter payment from customer: 
47.47
dollars 2
quarters: 1
dimes:  2
nickels: 0
pennies: 1

Notice how I am one penny off (same issue as the first example)? I'm relatively new to C++, but I have over a year of experience in Java, and I have never run into this weird of a truncation error when converting from double to int (I understand that the decimal points are lost in certain cases, but why the subtraction of a whole double value by exactly 1 in multiple instances?)

What changes to my code, specifically, would you recommend as to prevent this annoying truncation error?


Aucun commentaire:

Enregistrer un commentaire