Używanie typów zmiennoprzecinkowych do reprezentowania złotówek i groszy może doprowadzić do niepoprawnych wyników zobacz
IMHO lepszym pomysłem jest w tym przypadku operowanie na groszach reprezentowanych przez typ int, i przeliczaniu ich na złotówki tylko w momencie ich wyświetlania, co widać na dołączonym przykładzie.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int getMoney()
{
int money {0} , input {0};
cout << "Ile wrzuciles zlotych?" << endl ;
cin >> input;
money += input*100;
cout << "Ile wrzuciles groszy?" << endl ;
cin >> input;
money += input;
return money;
}
string printValue( int value )
{
string output {to_string(value/100)+" zl "};
output += to_string(value-(value/100)*100)+" gr ";
return output;
}
int main()
{
int price {1274};
auto money = getMoney();
while( money<price )
{
cout << "Za malo, dorzuc " << endl;
money += getMoney();
}
auto change {money-price};
cout << "Do wydania = " << printValue(change) << endl;
for( const auto& value : vector<int>{ 1000,500,200,100,50,20,10,5,1 } )
{
if( change>=value ) cout << value*0.01 << " zl x " << change/value << endl;
else continue;
change -= value*(change/value);
}
return 0;
}