Change Counter Works for Some Decimals Not Others

So I am trying to code a change counter in which a user enters an amount of change between 0.00 and .99, and the program calculates the minimum number of coins to make exact change (how many quarters, how many dimes, etc.) I feel like I've written sufficient code, especially as if compiles successfully and works for most amounts entered. The program correctly identifies the number of coins generally up to around 86 cents. At this point, the program begins adding an extra penny to most numbers (.99 is said to need 3 quarters, 2 dimes, and 5 pennies.) I'm coding and compiling in Dev-C++ 4.9.8.0. Please let me know what you think, many thanks!

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  const float Quarter = 0.25f;
  const float Dime = 0.10f;
  const float Nickel = 0.05f;
  const float Penny = 0.01f;
  int numQuarter = 0;
  int numDime = 0;
  int numNickel = 0;
  int numPenny = 0;
  float initAmt = 0.00f;

  cout << "Enter an amount of change between $0.00 and $0.99.  ";
  cin >> initAmt;
  
  if (initAmt >= 1.00 || initAmt < 0.00) {
    cout << "That number isn't between 0 and .99, please enter a new number.  ";
    cin >> initAmt;
    }
  
  while (initAmt > 0.00) {
    if (initAmt >= Quarter) {
        initAmt -= Quarter;
        numQuarter++;
    } else if (initAmt >= Dime) {
        initAmt -= Dime;
        numDime++;
    } else if (initAmt >= Nickel) {
        initAmt -= Nickel;
        numNickel++;
    } else {
        initAmt -= Penny;
        numPenny++;
    }
  }
  
  cout << "The # of quarters needed is: " << numQuarter << '\n'
       << "The # of dimes needed is: " << numDime << '\n'
       << "The # of nickels needed is: " << numNickel << '\n'
       << "The # of pennies needed is: " << numPenny << '\n'
       << endl;
         
  system("PAUSE");	
  return 0;
}
Topic archived. No new replies allowed.