Is it possible to use the modulus operator on a decimal?

Hey, so I'm trying to write this program in which once you are given change, it dispenses it according to highest tender. For example, if you purchased an item for \$2.50, it would give back 2 dollars and one 50 cent piece instead of 250 pennies.

So, the easiest way around this problem, I thought to myself, is using the modulus operator to store each digit so you could list it. The problem I'm having is that the modulus doesn't seem to work for the decimals.

Is there anyway around that? My code is below in case you are curious.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115`` `````` // Lab 3, Change // Programmer: Jesse Burns // Editor(s) used: JNotePad // Compiler(s) used: VC++ 2010 Express // The necessary C++ file library #include using std::cout; using std::cin; using std::endl; using std::ios; #include using std::string; #include int main() { // Jesse Burns, Lab 3 cout << "Lab 3, Change\n"; cout << "Programmer: Jesse Burns\n"; cout << "Editor(s) used: JNotePad\n"; cout << "Compiler(s) used: VC++ 2010 Express\n"; cout << "File: " << __FILE__ << endl; cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl; int a = 50; int b = 20; int c = 10; int d = 5; int e = 1; double f = 0.50; double g = 0.25; double h = 0.10; double i = 0.05; double j = 0.01; double change; int store50; int store20; int store10; int store5; int store1; double storefiftycents; double storetwentyfivecents; double storetencents; double storefivecents; double storeonecent; string buf; string buf1; string buf2; int x; int y; cout << "Enter the first number for the amount and a second to cover the purchase."<> buf1 >> buf2; cin >> buf; x = atoi(buf.c_str()); cin >> buf; y = atoi(buf.c_str()); change = x - y; cout << "The change back is " << change << endl; while (change == 0) { { change--; if (change < a) store50 = a % 10; if (change < b) store20 = b % 10; if (change < c) store10 = c % 10; if (change < d) store5 = d % 1; if (change < e) store1 = e % 1; if (change < f) storefiftycents = f % .1; if (change < g) storetwentyfivecents = g % .1; if (change < h) storetencents = h % .1; if (change < i) storefivecents = i % .01; if (change < j) storeonecent = j % .01; } cout << "Here is your change sir/madam. " << endl; cout << "\$50 :" << store50 << endl; cout << "\$20 :" << store20 << endl; cout << "\$10 :" << store10 << endl; cout << "\$5 :" << store5 << endl; cout << "\$1 :" << store1 << endl; cout << "50 cent piece :" << storefiftycents << endl; cout << "25 cent piece : " << storetwentyfivecents << endl; cout << "10 cent piece : " << storetencents << endl; cout << "5 cent piece : " << storefivecents << endl; cout << "1 cent piece : " << storeonecent << endl; } ``````
The modulus operator cannot be used for floating-point types such as float and double.

You can take advantage of the truncation that happens when casting a floating-point to an integral, e.g.
 ``123`` ``````float price = 2.50; int whole = price; //whole == 2 float change = price - whole; //change is about .50 ``````

Or you can use just integers to represent the number of cents.
 `` `` ``int price = 250, whole = price/100, change = price%100;``
The simplest way is to do the computations with integers (cents).

 ``123456789`` ``````\$2.60 == 260 cents 260 / 100 == 2 == two one dollar bills Remainder: 260 % 100 == 60 cents 60 / 50 == 1 == one fifty cent coin Remainder: 60 % 50 == 10 cents etc.``````

Though it is not required for this problem, std::fmod() computes the remainder of a floating point division.
http://en.cppreference.com/w/cpp/numeric/math/fmod

You can use fmod from <cmath>
http://www.cplusplus.com/reference/cmath/fmod/
 ``123456789`` ``````#include #include using namespace std; int main () { cout << "fmod of 5.3 / 2 is " << fmod (5.3,2) << endl; cout << "fmod of 18.5 / 4.2 is " << fmod (18.5,4.2) << endl; return 0; }`````` ```fmod of 5.3 / 2 is 1.3 fmod of 18.5 / 4.2 is 1.7```
Topic archived. No new replies allowed.