Perhaps you don't understand float imprecision fully enough. If you were to print the value of ch with more digits of precision, you would see what's happening. Try using the following:
This will show a more accurate value of what is in ch, rather than a rounded off value generated by the default precision resulting from the ...
This is a very common newbie problem. The problem lies here:
int main (void);
The semicolon is breaking the code. It must be removed. The correct form is this:
int main (void)
That semicolon marks the line as a function signature. When you get to functions, you'll understand this more. ;-)
If this answers your question, please ...
It looks like you've implemented it correctly. In the walkthrough, she is using 1 as an example, with the assumptions that it really is user 1 and that the user exists in the database. It also looks like she is leaving it to the student to determine how to get the user ID when it is unknown, along with all other values.
You could be getting an error for id ...
There are two problems with the code.
First, the output does not conform to the program specification. The spec calls for the total number of coins to be printed out and to use specific prompts. From the specification:
Your program should behave per the examples below.
Change owed: 0.41
Change owed: -0.41
Change owed: foo
Have you read the man page for the function? Maybe there's something your should include in your code that isn't there? ;-)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
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 ...
Your problem might be more logical than with just code. The pset seems to ask more how many quarters, dimes, nickels, and pennies at a minimum can you get to the exact change needed. This requires you to at least do some subtraction with n. You want to constantly subtract n with the maximum amount of cash you can subtract (25, 10, 5, or 1) without going ...
Time for some tough love. The logic in this code makes no sense. Take this for example:
int q = ((n / 25) % 10);
Say the amount given was $0.75. That makes n = 150. n/25 = 6. 6%10 = 6. But the actual number of quarters would be 3. I can't see how you got correct answers unless you just got lucky. I did run some tests and found some correct answers, ...
It's a simple problem and the same issue in all cases.
The code is trying to do dollar % 25 BUT, dollar is a float, not an int. The modulo operator requires both operands to be integers. Modulo cannot be applied to a float.
Maybe you meant to use owed instead? You'll need to declare owed before the do/while loop for it to stay in scope though.
If this ...
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?
It's a simple fix. Functions cannot be placed inside of main.
Just move the closing curly brace at the end to just above the line with the following comment:
// function gets the minimum number of coins needed;
This will close main() properly and allow functions to follow.
That should resolve this problem. Not saying there aren't other issues though.
The problem is that you're trying to store the float the get_float function gets from the user in a variable that you have initialized as an integer. That's truncating the float to the integer part and discarting the rest. You have to change the data type of that variable to a float. I hope this actually helps you.
At this point, it's very important that you develop your debugging skills, so I'll point you in the right direction.
What is the value of c after it is created and initialized but before the first while loop is executed, and the input is $4.20???
Can you explain why?
There are a few issues here.
First, it uses a floating point ( double n ) to store the amount of money. Then the code converts the dollar amount to cents (correctly) but then stores it back in the same floating point variable, thus defeating the purpose of eliminating the storage errors made by floating point numbers.
Next, the code uses do/while ...
This exercise expects the entire input amount to be converted to coin. It looks like the program will only convert the cents part of the input to coin. The check50 answers are correct. In the first test, input of 1.6, the cashier would give 6 quarters (.25) and 1 dime (.10), for a total of 7 coins.
More accurately, it isn't counting pennies correctly. In fact, it isn't counting a lot of things correctly. (Try $4.20) The cause of the problem is the whole purpose of this pset. Because of that, I'll suggest going back and reviewing the class lectures and other material related to the inaccurate storage of floats.
Hint: try printing out the value of f ...
There are serious issues with the way that you're trying to use IF statements. First of all, "n=x" is always true. A single "=" sign is an assignment operator. In other words, it always assigns the value on the right to the variable on the left, and will always evaluate as true.
If, instead, you want to check if two things are equal, you need to use the ...
Cut and paste error. What is the value of a dime? A nickel? A penny? I'm sure it isn't 25 cents each!
When you get to the point that you're not seeing the forest for the trees, as is the case here, it's always best to take a break and set it aside for at least a couple hours, and not think about it at all. Better to let it sit overnight!
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)
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 ...
Simply put, the code is printing the output without calculating the answer.
The code has a nice function called math(), but it is never called by main, so the calculation is never done.
Kudos for trying to implement a function at this point, but it's a bit early for that. At this point in your learning, I recommend doing all the coding inside main and ...
Most important question: did you resave the corrected code and remake it before checking it? The IDE does NOT automatically recompile your code.
I don't think it's an issue with the code above. I think it's an issue either with not saving it, not remaking/recompiling it, or you're editing one version and running another version in another directory.
It means that the value that the code is attempting to be stored in k is larger than what can be stored in this variable type.
The cause of this is an infinite loop. Lets look at that section of code.
while (r % 5 == 0)
Knowing that the amount was 15 cents, what is happening? When the code gets to here, r is still 15 because ...
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, ...
You have created an infinite loop. If the loop starts, it will never exit the loop. It tests the value contained in amount to decide to continue or exit, but since the value of amount is not changed inside the loop, it will never exit. You either need to change the value of amount inside the loop or you need to use an if statement instead, so that it only ...