# Why does only one of my four for loops execute?

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

• 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, 2015 at 1:22

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.