I was doing the Cash problem. I entered all the possible output given in the question yet its showing error. Please help, here is my code below . . . .

#include <cs50.h> #include <stdio.h>

int get_cents(void); int calculate_quarters(int cents); int calculate_dimes(int cents); int calculate_nickels(int cents); int calculate_pennies(int cents);

int main(void) { // Ask how many cents the customer is owed int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25 ;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters + dimes + nickels + pennies;

// Print total number of coins to give the customer
printf("%i \n", coins);


int get_cents(void) { int total_cent = get_int("Enter the total cents you have? "); while (total_cent < 0 ) { total_cent = get_int("Enter the total cents you have? "); } return total_cent; }

int calculate_quarters(int cents) { int q = cents/ 25; printf("%i\n",q); return q; } int calculate_dimes(int cents) { int d = cents / 10 ; printf("%i\n",d); return d; }

int calculate_nickels(int cents) { int n = cents / 5 ; printf("%i\n",n); return n; }

int calculate_pennies(int cents) { int p = cents / 1 ; printf("%i\n",p); return p; }

1 Answer 1


First of all, your code looks messy. To properly insert your code here - copy and paste it, then highlight your code and press "Code sample" {} button. I've cleaned your code for you. =)

And about error you've got - you missed part in problem description where it says that all input in check50 will be in 0.00 format:

  • Use get_float to get the user’s input and printf to output your answer. Assume that the only coins available are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢).
    • We ask that you use get_float so that you can handle dollars and cents, albeit sans dollar sign. In other words, if some customer is owed $9.75 (as in the case where a newspaper costs 25¢ but the customer pays with a $10 bill), assume that your program’s input will be 9.75 and not $9.75 or 975. However, if some customer is owed $9 exactly, assume that your program’s input will be 9.00 or just 9 but, again, not $9 or 900. Of course, by nature of floating-point values, your program will likely work with inputs like 9.0 and 9.000 as well; you need not worry about checking whether the user’s input is “formatted” like money should be.

So you need to change get_int to get_float in get_cents function and it's prototype. And then convert user input to integer.

  • Take care to round your cents to the nearest penny, as with round, which is declared in math.h. For instance, if dollars is a float with the user’s input (e.g., 0.20), then code like
int cents = round(dollars * 100);

will safely convert 0.20 (or even 0.200000002980232238769531250) to 20.

Good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .