Search code examples
ccs50relational-operators

Program that distributes change to customers. (C)


Goal:

Implement a program that calculates the minimum number of coins required to give a user change.

Issue:

Program does not execute properly. I'm fairly sure that my issue is logical in nature, but could also be resultant from my inexperience with proper formatting.

Extra information:

I added a print function to the code to determine where the problem was. "test1" is broadcast, "test2" is not.

I am using some functions local to the cs50.h library:

  • get_float(); - gets a float value from the user and stores it.

Please do not fix my code for me! I need to learn to do that myself. I just need help finding my logical error or formatting error. Yes, I know that it's not efficient.

Examples of what I'm looking for:

  • "Your issue is in line X, your values make it so that VARIABLE never reaches 0."

  • "In C, you can't format '( x < 0 );' - you have to say '(x<0);'."

Code walk-through:

Program obtains 2 float values from user, 'amount' and 'cost'. 'amount' is how much money was given by customer, 'cost' is the cost of the item.

Program finds 'amount - cost' to determine how much change is owed. This value is stored in 'diff'.

Program subtracts 0.25 from 'diff' and adds 1 to variable 'quarter'.

Program subtracts 0.10 from 'diff' and adds 1 to variable 'dime'.

...

Program prints how many Quarters, Dimes, Nickels, Pennies need to be used to give the customer change in the most efficient manner possible.

Assume that only coins can be used.

Code:

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


int main()
{

    float cost;
    do
    {
        printf("How much did it cost?\n");
        cost = get_float();
    }
    while (cost < 0);

    float amount;
    do
    {
        printf("How much did you pay?\n");
        amount = get_float();
    }
    while (amount < cost);




    int quarter = 0;
    int dime = 0;
    int nickel = 0;
    int penny = 0;
    float diff = amount - cost;
    do
    {
        while (diff >= 0.25)
        {
            diff = diff - .25;
            quarter++;
        }
        printf("test1");


        while (.10 <= diff < .25)
        {
            diff = diff - 0.10;
            dime++;
        }
        printf("test2");


        while (0.05 <= diff < .10)
        {
            diff = diff - 0.05;
            nickel++;
        }


        while (0.01 < diff < 0.05)
        {
            diff = diff - 0.01;
            penny++;
        }


        while (diff == 0.01)
        {
            penny++;
            diff = 0;
        }


    } // end bracket for do function
    while (diff > 0);



    if (diff == 0)
        {
            printf("Your change consists of:\n");
            printf("%i quarters.\n", quarter);
            printf("%i dimes.\n", dime);
            printf("%i nickels.\n", nickel);
            printf("%i pennies.\n", penny);
            exit(0);
        }

    if (diff < 0)
        {
            printf("Your change consists of:\n");
            printf("%i quarters.\n", quarter);
            printf("%i dimes.\n", dime);
            printf("%i nickels.\n", nickel);
            printf("%i pennies.\n", penny);
            exit(0);
        }

} // end bracket for int main

Expected result:

Program works as previously described.

Actual result:

Program does not execute fully. 'test1' is broadcast, 'test2' is not.


Solution

  • First of all, you need to check the accuracy for floating point arithmetic.

    That said, statements like

     while (.10 <= diff < .25)
    

    does not do what you think they do. Chaining of relational operators are not possible using the approach you used in C.

    You need to couple them using && operator, like

     while (.10 <= diff && diff < .25) {