Search code examples
cfunctionwhile-loopreturn-valuecs50

When running my function it won't return me a value, what am I doing wrong?


I'm currently working on the Cash assignment for Week 1 in CS50, and everything was running smoothly until I moved past quarters. The objective is to get the input form the user on how much change they owe, then create functions that find the least minimum change possible to give back (i.e. 60 cents is owed, then it would show 3 to represent 2 quarters and 1 dime). The top part is what was already provided:

#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);
}

Now I implemented the while function because I know I want it to do it only under a certain condition, but I can't get it to return the specific function back to the main. This is everything I've done:

int get_cents(void)
{
    int cents;
    do{
        cents = get_int("Change Owed: ");
    }
    while (cents<0);
    return cents;
}

int calculate_quarters(int cents)
{
    // TODO
    int quaters;
    while(24<cents)
    {
        quaters = cents/25;
    }
    return quaters;
}

int calculate_dimes(int cents)
{
    // TODO
    int dimes;
    while(9<cents)
    {
        dimes = cents/10;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    int nickels;
    while(4<cents)
    {
        nickels= cents/5;
    }
    return nickels;
}

int calculate_pennies(int cents)
{
    // TODO
    int pennies;
    while(0<cents)
    {
        pennies=cents/1;
    }
    return pennies;
}


Solution

  • At least in the function calculate_quarters you have an inifinte while loop when cents are greater than 24

    int calculate_quarters(int cents)
    {
        // TODO
        int quaters;
        while(24<cents)
        {
            quaters = cents/25;
        }
        return quaters;
    }
    

    because the variable cents is not being changed. It seems you need to define the function simply like

    int calculate_quarters(int cents)
    {
        return cents / 25;
    }
    

    Similar problems exist for other functions like calculate_dimes, calculate_nickels, and calculate_pennies.

    And this call

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

    is just redundant.

    Also try to use named constants instead of magic numbers like for example 25.

    Pay attention to that instead of using the signed type int for your variables it is much better to use unsigned integer type unsigned int to prevent using negative values.

    If to use the type int then your program can look simpler without defining the functions by means of the standard C function div declared in header <stdlib.h>

    For example

    #include <stdlib.h>
    
    //...
    
    
    // Calculate the number of quarters to give the customer 
    div_t result = div( cents, 25 );
    int quarters = result.quot;
    cents = result.rem;
    
    // Calculate the number of dimes to give the customer
    result = div( cents, 10 );
    int dimes = result.quot;
    cents = result.rem;
    
    // and so on
    

    In this case your program can look like

    #include <stdio.h>
    #include <stdlib.h>
    #include <cs50.h>    
    
    int get_cents( void )
    {
        int cents;
    
        do
        {
            cents = get_int( "Change Owed: " );
        } while (cents < 0);
    
        return cents;
    }
    
    int main( void )
    {
        const int CENTS_PER_QUATER = 25;
        const int CENTS_PER_DIME   = 10;
        const int CENTS_PER_NICKEL = 5;
    
        // Ask how many cents the customer is owed
        int cents = get_cents();
    
        // Calculate the number of quarters to give the customer
        div_t result = div( cents, CENTS_PER_QUATER );
        int quarters = result.quot;
        cents = result.rem;
    
        // Calculate the number of dimes to give the customer
        result = div( cents, CENTS_PER_DIME );
        int dimes = result.quot;
        cents = result.rem;
    
        // Calculate the number of nickels to give the customer
        result = div( cents, CENTS_PER_NICKEL );
        int nickels = result.quot;
        cents = result.rem;
    
        // Calculate the number of pennies to give the customer
        int pennies = cents;
    
        // Sum coins
        int coins = quarters + dimes + nickels + pennies;
    
        // Print total number of coins to give the customer
        printf( "%d\n", coins );
    }