# Thread: help with dividing doubles into integers

1. ## help with dividing doubles into integers

I am trying to compile this program but I keep getting an error message telling me that I'm converting doubles to integers, which I am trying to do, but I can't get it to compile. It's in the "else if " statement near the bottom. It is trying to divide the double "change" into the integers "twenties, tens, etc."
This is not important really but the program is supposed to implement a cash register and if the amount tendered is more the amount of the purchase it will show you how much change you owe, according to the number of twenties, tens, fives, etc.

Code:
```#include <iostream>

using namespace std;

int main()
{
cout << "Enter an amount equal to the amount of a purchase: \$";
double amount;
cin >> amount;
cout << endl << "Enter the amount tendered: \$";
double tender;
cin >> tender;
cout << endl;
double change = tender - amount;

if (tender < amount)
{
cout << "The amount tendered was \$" << tender << endl;
cout << "The amount of the purchase was \$" << amount << endl;
cout << "Please tender the additional amount of \$" << amount-tender << endl;
}

if (tender > amount && change == 0)
{
cout << "The amount tendered was \$" << tender << endl;
cout << "The amount of the purchase was \$" << amount << endl;
cout << "The total amount of change to be returned to the customer is \$" << change << endl;
}

else if (tender >amount && change > 0)
{
cout << "The amount tendered was \$" << tender << endl;
cout << "The amount of the purchase was \$" << amount << endl;
cout << "The amount of change to be returned to the customer is \$" << change << endl;
int twenties = change/20;
int tens = (change -twenties*20)/10;
int fives = (change - tens*10)/5;
int ones = (change - fives*5)/1;
int quarters = (change - ones*1)/.25;
int dimes = (change - quarters*.25)/.10;
int nickels = (change - dimes*.10)/.05;
int pennies = (changes - nickels*.05)/.01;
cout << "This will be returned as:" << endl;
cout << "Twenties: " << twenties << endl;
cout << "Tens: " << tens << endl;
cout << "Fives: " << fives << endl;
cout << "Ones: " << ones << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickels << endl;
cout << "Pennies: " << pennies << endl;
}

system("pause");
return 0;
}```

2. tender > amount && change == 0
How it can be?

Code:
`	int twenties = static_cast<int>(change)/20;`

3. Also, it's preferred to use getchar() instead of system("pause")
I also see vart react to
tender > amount && change == 0
Which brings up the question: if the customers pays more than the exact amount, how can the change be 0? You got a little logic problem there.

As for the doubles problem... Well, any decimals are floats or doubles. Anything like 0.20 is a double and anything like 0.20f is a float. And when you divide with a double or float, you'll get a float or double as result. And you can't assign a double or float to an int without an explicit cast. So either define all the variables as floats or truncate the result back into an integer via a cast.

4. wow thanks I completely messed up the logic there huh?

5. Yes you did... how can the given amount be more than the amount it costs and the change be 0?
So if I payed more than it cost, does that mean I get no change back?