0

I have been trying to solve the cash problem but i think i'm running into problems with scoping between prototype, main and function updating the cents variable. I have tried a variety of variations and mostly wind up with errors. This is the version that compiles:

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

int get_change(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickles(int cents);
int calculate_pennies(int cents);

int main(void)
{
        int cents = get_change();

        int quarters = calculate_quarters(cents);
        int dimes = calculate_dimes(cents);
        int nickles = calculate_nickles(cents);
        int pennies = calculate_pennies(cents);

        int coins = quarters + dimes + nickles + pennies;

        printf("Coins: %i \n", coins);
}

int get_change()
{
    int change = 0;

    do
    {
        change = get_int("Change owed: ");
    }
    while (change < 0);

    printf("Change input: %i \n", change);

    return change;
}

int calculate_quarters(cents)
{
    int quarters = 0;

    printf("Change before quarters: %i \n", cents);

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

    printf("Quarters: %i \n", quarters);
    printf("Change Remaining after quarters: %i \n", cents);

    return quarters;
}


int calculate_dimes(cents)
{
    int dimes = 0;

    printf("Change before dimes: %i \n", cents);

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


    printf("Dimes: %i \n", dimes);
    printf("Change Remaining after dimes: %i \n", cents);


    return dimes;
}

int calculate_nickles(cents)
{
    int nickles = 0;

    printf("Change before nickles: %i \n", cents);

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


    printf("Nickles: %i \n", nickles);
    printf("Change Remaining after nickles: %i \n", cents);


    return nickles;
}


int calculate_pennies(cents)
{
    int pennies = 0;

    printf("Change before pennies: %i \n", cents);

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


    printf("Pennies: %i \n", pennies);
    printf("Change Remaining after pennies: %i \n", cents);


    return pennies;
}
Output: 
Change Owed: 55
Change input: 55
Change before Quarters: 55 
Quarters: 2
Change remaining after quarter: 5 
Change before dimes: 55 
Dimes: 5
Change remaining after dimes: 5 
Change before nickles: 55 
nickles: 11
Change remaining after nickles: 0 
Change before pennies: 55 
Pennies: 55
Change remaining after pennies: 0 
Coins 73

1 Answer 1

0

Yes, it's a scope issue. When a function is called, parameters are passed by copy, not by reference. In other words, when the code calls a function like calculate_quarters(), it doesn't pass the original variable cents to the function. Instead, it makes a copy of the value in cents, and passes that copy to the function. The cents variable inside the function is a different var than the cents in main. (It wouldn't matter if the parameter variable in the function was called cents or foo. It's a different variable that has no effect on the original.)

That means that while the value of cents in the function may get updated, the original value of the different var cents in main remains unchanged.

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

1
  • Ok I figured it out. The cents inside each function is private scope and only used as a counter since its brought in as a copy. The value returned needs to update a variable outside of the function, but doesn't need to be a global one it can be updated in the main function. Thanks
    – Berry6400
    Commented Mar 4 at 12:18

You must log in to answer this question.

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