0

I'm having some trouble debugging my , program. It seems like the first for loop will run, but the program skips the next 3 and moves on to printing an incorrect value.

Thanks!

Nathan

1
  • glad your question is answered! you should probably update your question with the buggy code in order to complete the meaning of the question for reference by you or other people! thanks! – kzidane Jun 23 '15 at 1:22
1

I'm surprised it would even compile as shown. Let's say that you have previously defined q, d, n, and p though, that would allow it to compile. How you have done this will affect what results are generated.

In your for loops, q, d, n, and p are defined inside the loops. That means that the moment you exit the loops, they are lost, along with their contents. Assuming they were previously defined (so that it compiles), the problem remains that you are re-defining them, so a different version of each of them is used inside the loops and then lost, leaving the originals. That's a problem when you go to assign their values back to change. Let me illustrate with an example.

int x = 5;
for( int x = 10; x < 11; x++) 
{
    printf("inside loop - x = %i\n",x);
}        

printf("outside loop - x = %i\n",x);

The output will be:

inside loop - x = 10
outside loop - x = 5

Next, what happens when, for instance, q (or change) equals exactly .25? You'll close the quarter loop and go to the next loop while there is exactly 1 quarter left to be had.

Third, you are using a lot of floating point variables. Part of this lesson is to understand how floats are stored and how it can affect calculations. For instance, a number like 35.25 could be stored as 35.2499999997 or as 35.2500000003. (These are illustrative examples only. The architecture of the computer will dictate what exactly is stored.) This storage issue will wreak havoc on comparison tests like ==, >= and <= and can introduce errors in what you'd think is correct. That's why the exercise wants you to take the decimal input and convert to integers. I recommend that you review the class material and the assignment again to understand what is happening.

This should get you going.

BTW, it is OK to post the problematic part of your code, or even pseudocode to get help. Honor code just doesn't want you to do full code dumps. With the early assignments, having small amounts of code though, it can be difficult.

If this answers your question, please accept the answer to remove it from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

2
  • Thank you so much! I wondered if my variables were getting "trapped" inside the loop, but I thought defining them outside of the loop would fix the problem. Obviously not :). Thanks for reminding me to convert the floats back into ints, I honestly had completely forgotten about that. – Nathan M Jun 18 '15 at 23:19
  • Glad I could help, but, "... to convert the floats back into ints" I'm thinking you're heading down a dangerous path. Have you thought about taking the input amount and immediately converting THAT to an int and working in all integers? – Cliff B Jun 18 '15 at 23:24

You must log in to answer this question.

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