* Calculate the ideal change for a given transaction.
* 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.");
* Give the user change of the indicated amount.
* @param change_value the amount of change to give
void give_change(int change_value)
const int NUM_COIN_TYPES = 6;
const int TWO_DOLLARS = 200;
const int ONE_DOLLAR = 100;
const int FIFTY_CENTS = 50;
const int TWENTY_CENTS = 20;
const int TEN_CENTS = 10;
const int FIVE_CENTS = 5;
for (int i = 0; i < NUM_COIN_TYPES; i++)
coin_value = TWO_DOLLARS;
coin_value = FIFTY_CENTS;
coin_value = TWENTY_CENTS;
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");