0

My cash program won't return number that require pennies. For example, 5, 10, 15, 20, etc. will work fine, but 1, 2, 3, 4, 6, etc. will not print. I am guessing there is an issue with something related to pennies, but I can't diagnose the problem. Cheers.

#include <cs50.h>
#include <stdio.h>
#include <math.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 cents;
    do
    {
    cents = get_int("How many cents are owed?: \n");
    }
// keep asking the user for input while cents is less than 0
    while (cents < 0);
    return cents; //returned for use in other functions
}

int calculate_quarters(int cents)
{
    //determine num of quarters needed
    int quarters = 0;
    while (cents >= 25)
    {
        cents = cents - 25;
        quarters++;
    }
    // since this is a while loop, this will keep happening until cents >= 25
    return quarters;
}

int calculate_dimes(int cents)
{
    // determine number of dimes
    int dimes = 0;
    while (cents >= 10){
        cents = dimes - 10;
        dimes++;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    int nickles = 0;
    while (cents >= 5)
    {
        cents = cents - 5;
        nickles++;
    }
    return nickles;
}

int calculate_pennies(int cents)
{
    int pennies = 0;
    while (cents >= 1)
    {
        pennies = pennies - 1;
        pennies++;
    }
    return pennies;

}

1 Answer 1

0

Oops! Simple mistake hiding in plain sight. Look at this line from the calculate_pennies function:

    pennies = pennies - 1;

Shouldn't that be

    cents = cents - 1;

It's causing an infinite loop whenever pennies are involved because cents never gets changed.

Food for thought. Is the calculate_pennies function necessary? When you have already calculated quarters, dimes and nickels, isn't the number left in cents actually the number of pennies remaining?

This cures your penny problem. Are there any other issues? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Yup. Silly syntax error. Thanks for the help. All working now!!
    – pc510895
    Commented Oct 4, 2022 at 19:54
  • Is it? what do you get for 420? 18 or 19?
    – Cliff B
    Commented Oct 4, 2022 at 20:38

You must log in to answer this question.

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