The aim of this is to give an amount of change in the minimum number of notes/coins. So far, my code works fine for all values except those that include 2p or 1p coins, eg. 12.33 gives 1 £10, 1 £2, 1 20p, 1 10p, and 1 2p, but no 1p. I also tried just 2p which gives 1 1p and 1p gives 0 coins at all. I can't figure out what is wrong for these specific values, since the code is identical for each denomination.
#include <iostream>
using namespace std;
int main()
{
float change;
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;
int fiftyp = 0;
int twentyp = 0;
int tenp = 0;
int fivep = 0;
int twop = 0;
int onep = 0;
cout << "Please enter the amount of change given:\n";
cin >> change;
while ( change >= 50)
{
fifty++;
change -= 50;
}
while ( change >= 20)
{
twenty++;
change -= 20;
}
while ( change >= 10)
{
ten++;
change -= 10;
}
while ( change >= 5)
{
five++;
change -= 5;
}
while ( change >= 2)
{
two++;
change -= 2;
}
while ( change >= 1)
{
one++;
change -= 1;
}
while ( change >= 0.5)
{
fiftyp++;
change -= 0.5;
}
while ( change >= 0.2)
{
twentyp++;
change -= 0.2;
}
while ( change >= 0.1)
{
tenp++;
change -= 0.1;
}
while ( change >= 0.05)
{
fivep++;
change -= 0.05;
}
while ( change >= 0.02)
{
twop++;
change -= 0.02;
}
while ( change >= 0.01)
{
onep++;
change -= 0.01;
}
cout << "Give " << fifty << " £50 notes.\n";
cout << "Give " << twenty << " £20 notes.\n";
cout << "Give " << ten << " £10 notes.\n";
cout << "Give " << five << " £5 notes.\n";
cout << "Give " << two << " £2 coins.\n";
cout << "Give " << one << " £1 coins.\n";
cout << "Give " << fiftyp << " 50p coins.\n";
cout << "Give " << twentyp << " 20p coins.\n";
cout << "Give " << tenp << " 10p coins.\n";
cout << "Give " << fivep << " 5p coins.\n";
cout << "Give " << twop << " 2p coins.\n";
cout << "Give " << onep << " 1p coins.\n";
return 0;
}