* Calculate the ideal change for a given transaction.
* The different kinds of coins in the system
* The number of coins we can give change for
const int NUM_COIN_TYPES = 6;
* Each coin has a value and text. This is used to
* calculate the number of coins to give in change, and
* to determine what text to show the user.
* @field value The value of the coin
* @field text The coin text for output
* Read an integer from the user
* @param prompt the message to show the user
* @returns the integer entered
int read_integer(string prompt)
string line = read_line();
while (!is_integer(line))
write_line("Please enter a whole number.");
* Initialise a new coin of the indicated kind
* @param kind The kind of coin to setup
* @return coin data of the indicated kind
coin_data new_coin(coin_kind kind)
* Give the user change of the indicated amount.
* @param change_value the amount of change to give
void give_change(int change_value)
for (int i = 0; i < NUM_COIN_TYPES; i++)
coin_data coin = new_coin(coin_kind(i));
to_give = change_value / coin.value;
change_value = change_value - to_give * coin.value;
write(to_string(to_give) + " x " + coin.text);
string again = ""; // used to check if the user want to run again
int cost_of_item = read_integer("Cost of item in cents: ");
int amount_paid = read_integer("Payment in cents: ");
if (amount_paid >= cost_of_item)
give_change(amount_paid - cost_of_item);
write_line("Insufficient payment");
} while (again != "n" && again != "N");