Hot answers tagged

2

Get rid of the semicolon on line 6. A semicolon there marks the line as a function signature. You'll learn more about that later.


2

That single ; after your while is an empty statement, and in the code you show it is the loop's body. So your loop keeps doing nothing, and repeat that. Remove that extra semicolon. Semicola are meant to end statements (or separate the parts of a for loop), and there should be none between an if/while/for's controls or conditions and the thing executed ...


2

You gotta change your directory to where cash.c is located. cd pset1


2

Try printing out the value of cents after each calculation and think about the effect of integer division vs. regular division. Does the code track BOTH the number of coins for each denomination AND the change left over? Also, why is coins initialized to 1 and not 0?


1

The problem is that the code uses do/while loops when it should have used while loops. A do/while loop will ALWAYS run at least once. That means that it will add 4 coins and subtract at least 25 cents, 10 cents, 5 cents and 1 cent no matter what the amount is. If this answers your question, please click on the check mark to accept. Let's keep up on forum ...


1

The problem that you're seeing is here: quantity = round (quantity * 100); So, what is the value of quantity before this line execute? What variable actually contains the value of the change to be counted? There's another issue in the code, but it's an easy fix. I'll let you find that after you fix this. ;-) If this answers your question, please click ...


1

Why are you using modulo for this? It produces an unreliable result in this usage. Further, the actual numbers don't make sense. The modulo function returns the remainder after an integer division. So, for example, say that you're doing while (cents % 10 == 5) If cents were $1.05, it would be true, but if cents were $1.06, it would be false. In both ...


1

Could it be the missing line break? The output of printf("Your input: $ %.2f", a); printf(", which amounts to %i total.\n", cents); or equivalently printf("Your input: $ %.2f, which amounts to %i total.\n", a, cents); ends in a newline \n, so the next number would be placed on the next line. Without that second part, whatever you print next gets printed ...


1

Why does the code increment the amount of change, i, each time it subtracts the value of a coin from it? You should be using a separate variable to track the number of coins.


1

By definition, the function named main does not require a return. From this doc If the returned type is compatible with int and control reaches the terminating }, the value returned to the environment is the same as if executing return 0; Check the spelling in the program here: int maid (void)


1

Its good that you're thinking about how to make the code more efficient. Kudos to you! Yes, repeating code is a red flag that things can be simplified. But here, the code is very similar but not the same. It's only the same pattern performed on different variables. It's good that you recognize that there may be a better way, but it's also very important to ...


1

Welcome to the world of (binary) floating point. A value like 4.2 cannot be precisely represented in binary, it would be represented by an infinite sequence of binary digits. Obviously we don't have an infinite amount of RAM, so the number is cut off at some point, resulting in a binary number that's slightly above or below the value you wanted to express, ...


1

You already understand all the parts of what's happening. Time to put them all together. The round function always rounds at the decimal. It's a simple fix. Move the decimal before you round. Don't round before you move the decimal (as the code does now.) If this answers your question, please click on the check mark to accept. Let's keep up on forum ...


1

It's doing exactly what it was told. Remember the differences between a while loop and a do/while loop. A while loop can only execute if the condition is true because the test condition is checked BEFORE each pass. A do/while loop must execute at least once because the test condition is only checked AFTER each pass. Maybe a review of the two types of ...


1

You have changed to using cents, but quarter and so on are still in dollars. Variables like count, declared in the function, not globally, are not initialised by default, so start at any value (maybe zero, but maybe not). Explicitly initialise those, like int count = 0;. Third, you probably meant cents >= quarter, not >. Also, good luck with 4.2. ...


Only top voted, non community-wiki answers of a minimum length are eligible