Hello everyone I am in need of some help.. I thought that I did my assignment the way that my professor wanted but I just found out that he wants me to solve it differently.
Problem:
Given any amount of money expressed in dollars, and cents, this program computes the number of 100 , 50, 20, 10, 5 and 1 dollar bills and number of quarters, dimes, nickels, and pennies to be returned, returning how many of each denomination are included.

I figured out how to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.
When converting that number to int it returns it as 14.50. Does anyone know how to handle converting doubles, similar to the one above, to an integer correctly?

This is the code my teacher did not want because I treated the input as a string. Thanks in advance for your help!

``````#include <iostream>
#include <string>
#include<iomanip>
#include<windows.h>

using namespace std;

int main(){
const int hundredsConst=10000, fiftiesConst=5000, twentiesConst=2000, tensConst=1000;
const int fivesConst=500, onesConst=100, quartersConst=25, dimesConst=10, nickelsConst=5, tab=5;
int hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies;
int dollarsBeforeDecimal, centsAfterDecimal, moneyInPennies, counter=1;
string name, hundredsEnd, fiftiesEnd, twentiesEnd, tensEnd, fivesEnd, onesEnd, quartersEnd, dimesEnd, nickelsEnd, penniesEnd;
char decimalPoint;

cin >> dollarsBeforeDecimal >> decimalPoint >> centsAfterDecimal;
moneyInPennies = (dollarsBeforeDecimal*100) + (centsAfterDecimal);

hundreds = moneyInPennies/hundredsConst;
moneyInPennies -= hundreds*hundredsConst;

fifties = moneyInPennies/fiftiesConst;
moneyInPennies -= fifties*fiftiesConst;

twenties = moneyInPennies/twentiesConst;
moneyInPennies -= twenties*twentiesConst;

tens = moneyInPennies/tensConst;
moneyInPennies -= tens*tensConst;

fives = moneyInPennies/fivesConst;
moneyInPennies -= fives*fivesConst;

ones = moneyInPennies/onesConst;
moneyInPennies -= ones*onesConst;

quarters = moneyInPennies/quartersConst;
moneyInPennies -= quarters*quartersConst;

dimes = moneyInPennies/dimesConst;
moneyInPennies -= dimes*dimesConst;

nickels = moneyInPennies/nickelsConst;
moneyInPennies -= nickels*nickelsConst;

pennies = moneyInPennies;
return 0;
}
``````

14.49999999999999999999999999999999.

What's the problem? There is no such kind of money like that, it a rounding error. 14.50 is the correct value.

Does anyone know how to handle converting doubles, similar to the one above, to an integer correctly?

To convert 14.50 to int (1450) just multiply by 100 and assign it to an integer.

Unless you use an arbitrary precision library (such as Boost), this is not possible. All floating point (including double types) are imprecise and cannot be used easily for precise values as can integer types. So, use integers to express dollars and cents. Use the modf() function to extract the dollars and cents into integers from a floating point representation of a value, and finally, use the modulus operator to extract the denominations from each. I will leave the actual solution to you, but post your next effort here and we will probably help you sort it all out.

Well the profesor wants to be able to test crazy values like that for some reason.. I honestly have no idea what to do for this. He does not want us to use any advanced operations since we have just started the semester so I don't think your suggestion will work rubberman. This is what I got so far

``````#include <iostream>
#include <string>
#include<iomanip>

using namespace std;

int main()
{
float start_money;
int int_money, moneyInPennies, centsAmt, dollarsAmt, hundreds, fifties, twenties, tens, fives, ones;
int quarters, dimes, nickels, pennies;
const int hundredsConst=10000, fiftiesConst=5000, twentiesConst=2000, tensConst=1000;
const int fivesConst=500, onesConst=100, quartersConst=25, dimesConst=10, nickelsConst=5, tab=5;
string name, hundredsEnd, fiftiesEnd, twentiesEnd, tensEnd, fivesEnd, onesEnd, quartersEnd, dimesEnd, nickelsEnd, penniesEnd;

cout << "Please enter your full name: " << setw(tab) << ' ';
getline(cin, name);
cout << "\nHello " << name << " how much do you want to change?" << setw(tab) << ' ' << '\$';
cin >> start_money;

int_money = start_money*100;
cout << int_money << endl;
centsAmt = (int_money % 100);
dollarsAmt = (int_money - centsAmt);
moneyInPennies = (dollarsAmt + centsAmt);

hundreds = moneyInPennies/hundredsConst;
moneyInPennies -= hundreds*hundredsConst;

fifties = moneyInPennies/fiftiesConst;
moneyInPennies -= fifties*fiftiesConst;

twenties = moneyInPennies/twentiesConst;
moneyInPennies -= twenties*twentiesConst;

tens = moneyInPennies/tensConst;
moneyInPennies -= tens*tensConst;

fives = moneyInPennies/fivesConst;
moneyInPennies -= fives*fivesConst;

ones = moneyInPennies/onesConst;
moneyInPennies -= ones*onesConst;

quarters = moneyInPennies/quartersConst;
moneyInPennies -= quarters*quartersConst;

dimes = moneyInPennies/dimesConst;
moneyInPennies -= dimes*dimesConst;

nickels = moneyInPennies/nickelsConst;
moneyInPennies -= nickels*nickelsConst;

pennies = moneyInPennies;

if(hundreds > 1 || hundreds <=0)
hundredsEnd = 's';
if(fifties > 1 || fifties <=0)
fiftiesEnd = 's';
if(twenties > 1 || twenties <=0)
twentiesEnd = 's';
if(tens > 1 || tens <=0)
tensEnd = 's';
if(fives > 1 || fives <=0)
fivesEnd = 's';
if(ones > 1 || ones <=0)
onesEnd = 's';
if(quarters > 1 || quarters <=0)
quartersEnd = 's';
if(dimes > 1 || dimes <=0)
dimesEnd = 's';
if(nickels > 1 || nickels <=0)
nickelsEnd = 's';
if(pennies > 1 || pennies <=0)
penniesEnd = 's';

cout << "\nYour change is: " << endl;
cout << endl;
cout << setw(tab) << ' ' << hundreds << " Hundred dollar bill" + hundredsEnd << endl;
cout << endl;
cout << setw(tab) << ' ' << fifties << " Fifty dollar bill" + fiftiesEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << twenties << " Twenty dollar bill"+twentiesEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << tens << " Ten dollar bill"+tensEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << fives << " Five dollar bill"+fivesEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << ones << " One dollar bill"+onesEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << quarters << " Quarter"+quartersEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << dimes << " Dime"+dimesEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << nickels << " Nickel"+nickelsEnd<< endl;
cout << endl;
cout << setw(tab) << ' ' << pennies << " Pennie"+penniesEnd<< endl;
return 0;
}
``````

Does your teacher want you to take the value as-is or round it? Regardless, you need to keep only the first two digits after the radix for the purposes of this program, but whether you round (and how you round) can affect the result.

A naive approach (which should be sufficient for this exercise) is to extract the two digits after the radix and convert it to `int`. Then look for the third digit after the radix and if it's greater than 5, round up. This should give you a decent facsimile of the rounding that `cout` performs:

``````#include <iostream>

using namespace std;

int main()
{
double value = 14.49999;
int ivalue = (int)((value - (int)value) * 1000);

// Remove the dollar amount and convert cents to int
int cents = ivalue / 10;

// Find the rounding delta
int delta = ivalue % 10;

cout << "Cents: " << cents << '\n';
cout << "Delta: " << delta << '\n';

if (delta >= 5) {
++cents;
}

cout << "Cents rounded up: " << cents << '\n';
}
``````

If you don't need to round, then you can take the cents as-is and call it good. But financial applications typically round up. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.