0

I am using while loops (every time change is greater than quarter, have coins used added by one and subtract the amount of change by the amount just taken away). All of my values are converted to ints, but I constantly get a print value of 0. I have used python and these programs are very easy to make but this is frustrating me.

1
  • Did you use round()? How did you convert your values to ints?
    – kzidane
    Commented Jul 28, 2014 at 2:46

3 Answers 3

2

A correct way is to avoid dealing with floats because they're not precise and to deal with ints instead. To do that you should

  1. convert the amount you receive from the user in dollars (the float) to be in cents.
  2. round the amount (in cents) to the nearest integer and store it in an int variable.
  3. calculate the number of coins and output it followed by a newline character.

Casting floats to ints truncates the digits after the decimal point. So if you're casting a value between 0.0 and 1.0 to an int, it will be stored as 0. Besides, because floats are not precise, even the whole numbers like 1.0 or 2.0 can be stored as 0.9999999 and 1.9999999 respectively. When you then cast these to ints, they'll be stored as 0 and 1 respectively (not 1 and 2 as they should).


Update: after reviewing your code, I could identify the following bugs

  1. you shouldn't be reading the float like that

    scanf("%.2f\n", &change);
    
  2. you're not rounding the amount you get from the user.

  3. you have logical errors in your code.
  4. you shouldn't be printing the value of change.

How to fix these?

  1. you should rather be reading the float either like this

    change = GetInt(); // from the cs50 library
    

    or this

    scanf("%f", &change);
    
  2. use the round() function (declared in math.h) to round the amount that you receive from the user.

  3. given the following piece of code

    while (condition)
        printf("hello from inside the loop!\n");
    
    printf("hello from outside the loop!\n");
    

    As you might have noticed, when you don't use curly braces (i.e., { and }) to enclose the body of the loop, only the first statement right after the loop is the one that's inside that body of this loop (and that's gonna keep executing as long as condition is true). All the following statements are out of the body of the loop and are executed anyway. You're having many similar problems like this in your code.

    Also, the loops that calculate the number of dimes and nickles shouldn't be nested inside the loop that calculates the number of quarters because if initially change is < quarter, the body of this loop is never executed. And thus, we won't calculate the number of coins correctly (and that's the one that might be causing your program to print zeros).

  4. you should remove all printf() calls that print the value of change.

3
  • This is exactly what I did, I converted all of my values to ints by multiplying the amount of change by 100 and the values of each coin by 100. Every time change is used I would add one to the amount of coins used but I still print 0, which is puzzling.
    – Daniel
    Commented Jul 28, 2014 at 19:06
  • @Daniel you may send me your source code at [email protected] to see if I can help you identify the bug!
    – kzidane
    Commented Jul 28, 2014 at 21:54
  • @Daniel I've received your email, reviewed your code and updated my answer. Please look at the update section! Feel free to ask for more details and/or to ask more questions!
    – kzidane
    Commented Jul 30, 2014 at 3:35
0

Zamyla in the walkthrough suggests using the round function.

float roundf(round x) helped me.

You can see how to use it by typing "man round" into your terminal. "man round" will bring up the manual (man) page for round.

Round is included in the math.h library so don't forget to add that to your program if you use this function.

Check it out here: http://d2o9nyf4hwsci4.cloudfront.net/2014/x/psets/1/pset1/pset1.html#_time_for_change

It will get rid of that pesky truncating issue.

0

If you always have a printed value of 0, odds are that either your printf() has an issue on the specified variable to be printed (i.e.you ask to print a wrog variable), or you don't update your change_due variable in the correct way. .

You must log in to answer this question.

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