|
-
October 4th, 2010, 04:51 PM
#1
double to int issue. losing a penny
For class I have to write a program that will take in the conversion rate from euros to dollars and the amount of euros the person has. Then it is supposed to convert to US dollars and then distribute the money into dollars, quarters, dimes, nickels, and pennies.
So far I can get everything to work but I lose a penny.
For example I enter in the conversion rate of .7266 (rate as of last night) and put in that I have 5000 euros.
I get 6881.37 US dollars.
with 6881 dollars, 1 quarter, 1 dime, 0 nickes, 1 penny. This adds up to 6811.36.
I am not really sure why this is happening.
Here is the code
==============
#include <iostream>
using namespace std;
int main() {
cout << "Hello, travelers! \n\n" << "Enter the conversion rate from euros to dollars (i.e. how many euros in a US dollar):";
double numEurosPerDollar = 0;
cin >> numEurosPerDollar;
cout << "\n\nThe conversion rate you entered means that one US dollar is equivalent to " << numEurosPerDollar << " euros today. \n\n";
cout << "Now enter the total amount you'd like to convert from euros to USD (up to hundreths of a euro):";
double numEuros = 0;
cin >> numEuros;
double numDollars = numEuros / numEurosPerDollar;
cout << "\n\n" << numEuros << " euros will convert to " << numDollars << " US dollars.\n\n";
cout << "If you were to make this from change, using the biggest denomination at every step, it would be:\n\n";
const int PENNIES_PER_DOLLAR = 100;
const int PENNIES_PER_NICKEL = 5;
const int PENNIES_PER_DIME = 10;
const int PENNIES_PER_QUARTER = 25;
int numPennies = numDollars * PENNIES_PER_DOLLAR;
int numPenniesLeftOver = numPennies % PENNIES_PER_DOLLAR;
//this gives us whole dollars and left over pennies.
int numQuarters = numPenniesLeftOver / PENNIES_PER_QUARTER;
numPenniesLeftOver = numPenniesLeftOver % PENNIES_PER_QUARTER;
//this gives us whole quarters and left over pennies.
int numDimes = numPenniesLeftOver / PENNIES_PER_DIME ;
numPenniesLeftOver = numPenniesLeftOver % PENNIES_PER_DIME;
//this gives us whole dimes and left over pennies.
int numNickel = numPenniesLeftOver / PENNIES_PER_NICKEL;
numPenniesLeftOver = numPenniesLeftOver % PENNIES_PER_NICKEL;
//this gives uf whole nickels and left over pennies.
cout << (int)numDollars << " dollars, " << numQuarters << " quarters, " << numDimes << " dimes, " << numNickel << " nickels, and " << numPenniesLeftOver << " pennies.";
cout << "\n\n";
return 0;
}
============
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|