0

I'm very new to coding and I'm stuck on the cash problem set. It keeps returning a very high number and not returning cash. my code is 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 cents;
   do
   {
    cents = get_int("change owed: ");
   }
   while (cents <= 1);
   return cents;
}

int calculate_quarters(int cents)
{
    int quarters;

    while (cents >= 25)
    {
      quarters = (cents / 25);
      quarters++;
    }
    return quarters;
}

int calculate_dimes(int cents)
{
    // TODO
    int dimes;

    while(cents >= 10)
    {
        dimes = (cents / 10);
        dimes++;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    int nickles;

    while(cents >= 5)
    {
       nickles = (cents / 5);
       nickles++;
    }
    return nickles;
}

int calculate_pennies(int cents)
{
    // TODO
    int pennies;

    while(cents >= 1)
    {
        pennies = (cents / 1);
        pennies++;
    }
    return pennies;

}

1 Answer 1

1

It looks to me like it's stuck in an infinite loop. Let's look at one of your functions.

int calculate_quarters(int cents)
{
    int quarters;

    while (cents >= 25)
    {
      quarters = (cents / 25);
      quarters++;
    }
    return quarters;
}

When this function is called, it drops into the while loop, assuming cents is >= 25. That's fine.

The problem is inside the loop. The logic is flawed in a couple ways. First, it calculates the total number of quarters in cents. That would be fine if that was all that was happening. Next, it increments quarters, making the number of quarters wrong. That's one issue.

The next, bigger issue is that this is a loop. The loop will only end when cents is less than 25. The problem with that is that the value of cents never changes, so the loop will never end!!!

Think about exactly what the code does. It often helps to work it out on paper and calculate the vars as you step through it in your head.

Looking at the big picture, it appears that you've tried to implement two different algorithms here. One algorithm uses a loop to subtract 25 from cents and increment quarters counter on each pass of the loop. (BTW, not terribly efficient.) The other algo is to simply calculate how many quarters are in cents using integer division. (This is a single statement execution and much more efficient.) You need to pick one algorithm.

Kudos for practicing the use of functions. However, when functions are used only once, it's often better to incorporate the code in the calling code instead of creating a function.

This should get you going. Happy coding! ;-)

BTW, is it necessary to calculate the number of pennies, or even have a function for it??? Think about why not. ;-)

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

1
  • This helped so much, thank you!!
    – faeyb
    Commented Oct 11, 2023 at 15:50

You must log in to answer this question.

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