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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
   // Lab 3, Change
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express

// The necessary C++ file library
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

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."<<endl;
  cin >> 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.
1
2
3
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).

1
2
3
4
5
6
7
8
9
$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/
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cmath>
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.