0

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
krount
  • 15
  • 2
  • 3
    Side note: when working with currency, use integers and convert to the smallest denomination. Example: US dollars to US cents. ($1.00 -> 100). This avoids floating point errors. – 001 Dec 22 '17 at 13:59
  • This is a really good method! Thank you. – krount Dec 22 '17 at 14:13

3 Answers3

0

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) {
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Wow, I definitely understand float values better now. A chain of relational operators is just a Boolean expression at its core, I suppose - I really need to start breaking down these concepts. This helps a lot. Thank you! – krount Dec 22 '17 at 14:16
0

The trouble is with comparisons like 0.05 <= diff < .10 which are not the same as 0.05 <= diff && diff < .10 The latter does what you intend, the former compares 0 or 1 to .10. If diff is greater than 0.05, then '0.05 <= diff' evaluates to 1, which is then compared to .10. What you've written is the same as (0.05 <= diff) < .10.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thank you! I definitely understand why my former code wasn't working. I also thought of some ways I can use this notation for future projects! This helps a lot - thank you! – krount Dec 22 '17 at 14:18
0

First of all, your loop is eternal. while (amount < cost); <-- Amount and cost never change. So...

Second of all, you can not write conditions like (1 <= 2 < 3), it does not mean what you think it means. It seperately executes the operators, so you end up with something akin to (1 <= 2) < 3, the first being either a 1 (true) or a 0 (false).

  • I wanted to prompt the user to provide a different 'amount' value if the amount entered was less than 'cost'. It doesn't really make sense for them to receive change if they paid less than the item cost. It's definitely not user-friendly in doing this though. Is there issue in this? Would it be better to use an if-else statement? – krount Dec 22 '17 at 14:20