# Can't get the loop to work

This is a discussion on Can't get the loop to work within the C Programming forums, part of the General Programming Boards category; Code: /* The Variables: n = the initial depth u = the height I can climb in 1 minute d ...

1. ## I posted my logic, can anyone point me inthe right direction

Code:
```/* The Variables:
n = the initial depth
u = the height I can climb in 1 minute
d = the number of feet I slip back during rest
assume d < u and n < 100
a value of n = 0 indicates end of input
Each input instance should generate a single line output to
indicate the number of minutes it would take to climb out of the shaft.
Sample input
10 2 1
Sample output
17 */

#include <stdio.h>
#include <stdlib.h>

int main()
{

int u,d,n,c = 0;
int t,r,m,firsttry = t, firstrest = r, minutes = m , counter = c;

scanf("%d %d %d", &n, &u, &d);
getchar();

if (n >100 )  // If you enter a number greater then 100

for (n != 0; c >= 0; c = c++)// If n is not equal to 0, then loop till it is
{                          // c is the counter for m
m = c ;
t = n - u; // t is the attempts where depth minus climb = the new heigth
r = t + d; // r is the distance I slip back while resting
// where the try + the drop equals the new heigth
//IF I put a print statement here we get the correct answer
{      // on the first try, so there is a problem with the loop
//printf("%d", r); gives me 99 which is correct for the first drop
getchar();
if  (r <= 0)  // So if r is equal to 0, I am out of the shaft
printf("It took %3.0f minutes", m); // display the counter m
else          // OR ELSE
n = r;        // r is given the value of n for the loop
getchar();
}
}
return 0;
}```
The problem i am having is to get the loop and the counter to work,I can't see where the looping problem exists,, should my if statement be changed to a while or a for statement or should i abondan both and use an array?.. If i were to use an array how would i initialize it.

Too many questons , my head hurts..lol sorry, any direction on this will be welcome.

Thanks a bunch...

2. First off, stop making new threads for the same exact topic. Second, you never change the value of c, other than to increment it. Your loop is going to take forever, until you overflow the + signed end of your variable, and it wraps over to a negative number.

Quzah.

3. ## C counter

The c is just a counter for the number of loops.. or when i wrote it in the code did i make it something else?..

Is it possible that my logic is correct but i didn't use the right statements to make it work?

Can someone point me int he right direction?
Thanks

4. Also, I notice you're using a boolean expression for the initial statement in your loop.
Code:
```  for (n != 0; c >= 0; c = c++)// If n is not equal to 0, then loop till it is
{                          // c is the counter for m```
You say if c is not equal to 0 in your comment, but its actually if c is greater than or equal to 0. Big difference, and that might be why the loop isn't working the way you want it to. What exactly is your logic behind those three statements? Forget the code, what should it be doing.

5. you can't put a comparison in your for loop. You need to put an
Code:
```if(n!=0){
for( ; c >= 0; c++){
...
}
}```
you also don't need the c=c++ it automatically increments the value of c

6. Or you could do like so, if that's what you meant to do.
Code:
`for(; n != 0 && c >= 0; ++c)`

7. You still have the problem of "c" looping some 2 BILLION times. On the off chance you've got an old compiler (What am I saying? EVERYONE here uses old compilers.) and have 16bit ints, your loop will still execute some 32000 times...

Quzah.

8. ## The Logic

The logic is that a person falls down an elevator shaft N number of feet, climbs up U number of feet/minute, then rests for one minute and slips back down D number of feet.

So t = N - U which is the fall plus how far you climbed
r = t + D which is the resultant of t + how far you slid back down

now r is the new height you are at when you start climbing again. Therefore in reality the value in r is the new N. So N = r for the loop.

now i used c strickly for a counter of how many loops were performed to get N to equal 0.

My thinking is that if r <= 0 then display the value in c.
If its not then N = r and start the loop over till it is.

My logic seems ok, its how i coded my work, i am not sure i even started it right other then having the equations for t and r which work proper. I need a counter for the number of loops which is displayed as minutes. That is why i made the for loop (n != 0; c >= 0; c = c++) using c as the counter.

I have really fallen down on this one, and can't seem to see what it is that i require. That is why i posted it here for direction on where to look. If my coding is started wrong for my logic i can accept that, this is all new to me, when i was in high school computers were punch cards.. so any help, or suggestion is really needed and greatly appreciated..

Thanks..

9. <brief derailing>
Originally Posted by linuxdude
you also don't need the c=c++ it automatically increments the value of c
In fact, shouldn't the expression c=c++ cause undefined behavior, since you're modifying c twice between sequence points?
</brief derailing>

10. Code:
`c=c++;`
That code would set c to the value of c, and then increment c. You might as well just put

Code:
`c++;`
In fact, shouldn't the expression c=c++ cause undefined behavior . . . ?
No, I don't think so.

11. /* The Variables:
n = the initial depth
u = the height I can climb in 1 minute
d = the number of feet I slip back during rest
assume d < u and n < 100
a value of n = 0 indicates end of input
Each input instance should generate a single line output to
indicate the number of minutes it would take to climb out of the shaft.
Sample input
10 2 1
Sample output
17 */
So, like this?

Code:
```int main() {
int n = 10, u = 2, d = 1;
int x = 0;

for( ; n > 0; x ++) {
n -= u;
u += d;
}

printf("%i", x);

return 0;
}```

Of course the above code would crash if you have a negitive depth or you slip down more than you climb up;

Code:
```int main() {
int n = 10, u = 2, d = 1;
int x = 0;

if(n <= 0) printf("You're already out.");
if(u <= d) printf("You'll never get out.");
else {
for( ; n > 0; x ++) {
n -= u;
u += d;
}

printf("It will take you %i minutes to escape.", x);
}

return 0;
}```