Quote:
What's wrong with this C code?
Well, apart from the comments being pretty much useless ... Never comment what the code is doing, people can read that - comment why it's doing it if it needs commenting:
int cents = get_cents();
The comment is longer than teh code, and isn't even accurate - it presumably prompts the user for the cents owed, and gets his response. The comment doesn't reflect that so it's both unnecessary and misleading!
All your comments are like that ... but you don't comment the functions themselves, where the comment could be useful if it was accurate:
int get_cents(void)
{
int cents;
do
{
cents = get_int("Change owed: ");
}
while (cents < 0);
return cents;
}
Apart from that, we have no idea what you consider "Wrong": we don't know what data you are testign it with, or what the actual purpose of the code is (the assignment you were given covers what exactly your tutor wants you to submit, and we can't see that).
So assuming that
cs50.h
holds the definition of
get_int
so your code compiles (and that's another thing we can't see) I'm going to assume that it runs, but gives you unexpected results of some kind.
Which means it's time to find out why - and we can't do that because we can't run your code, and don't have your test data if we could!
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!