0

I'm not sure why QUARTER is not being subtracted from Owed_Change and updating it?

As of it it will take user input, convert to an int and round it, for some reason it is ignoring my While loop.

Any help would be much appreciated. Below is the my current code:

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

int main(void) { 

 const int QUARTER = 25; 
 int CoinCounter = 0; 

 printf("Please enter change required: ");

 float Change = GetFloat(); 

    Change *= 100;

    Change = round((int)Change);

 int Owed_Change = Change;  


 while (Owed_Change  =>  QUARTER) {    //<---- This is killing me. 
       Owed_Change -= QUARTER;
 }

    ++CoinCounter;
    printf("%i\n" "%i ", Owed_Change, CoinCounter); 
 } 

This is my current revised code:

 int main(void) { 

 const int QUARTER = 25; 
 int CoinCounter = 0; 

 printf("Please enter change required: ");

 float Change = GetFloat(); 
 Change *= 100;
 int a = round(Change);

 int Owed_Change = a;  

 while (Owed_Change  >=  QUARTER) { 
        Owed_Change -= QUARTER;
        ++CoinCounter;
   }


    printf("%i\n" "%i ", Owed_Change, CoinCounter); 
   } 

Please enter change required: 10.45
20
41 ~/workspace/ $ 

If my math is correct this would be: 10.45 * 100 = 1045 - 25 = 1020, Why is it printing Owed_Change as 20? I notice it also prints the CoinCounter -1 coin.

2

Well, you have a couple of issues. I'm assunimg this is incomplete code and that you're just testing for quarters at this point, which is a good approach.

First look at the following:

        Change = round((int)Change);

Now, think about the order of execution. Operations are executed from the inside of parentheses to the outside. So, Change which is a float, is cast as in int first. When a float is cast as an int, it is truncated. That means that 2.99999 becomes 2, not what you intended. Next, the round function executes. In this case, it won't do anything because a truncated integer will be unchanged after being rounded, no matter what the value. (Note too that round() returns an integer, so the int casting is unnecessary.) Finally, it's stored back in a float, which reintroduces the problem with approximated storage in a computer. It should be stored directly in a var of type int.

Next, your while loop is subtracting quarters, but it isn't incrementing the coin count inside the loop. The coin count is only being incremented after the loop and only once.

[EDIT: followup after question edit]

The reason the revised code outputs 20 for Owed_Change is because that's all that's left. Remember, it's running a loop, so it will subtract 25 over and over until Owed_Change is less than 25. It also increments CoinCounter each time it subtracts 25. So, it is correctly outputting 41(quarters) and 20. So, (41 * 25) + 20 = 1045. So add the remaining code for dimes, nickels and pennies to clear the rest of the change!

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

| improve this answer | |
  • First off, thank you so much for your feedback. Prior to seeing your response, I had only changed the binary op. and the code gave me the expected output. I entered change at .43 cents and it returned an int of 18? Im not sure why though? – ncubed Jan 2 '17 at 23:02
  • Think about it. The code multiplies .43 * 100 and then subtracts 25. That leaves 18. Unfortunately, that's not the number of coins. Also, depending on the starting number, sometimes the issues I described will happen, other times they won't. Try $4.20 – Cliff B Jan 2 '17 at 23:20
0

We have a small syntax error the binary operator "greater than or equal to" is written

>=

And no =>

| improve this answer | |
  • ...and yes, that is a typo! (Wanna bet it's a transcription error? ;-) ) – Cliff B Jan 2 '17 at 22:44

You must log in to answer this question.

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