Why is while loop producing an extra loop ?

So Im having trouble with this code - while loop makes an extra loop at the end, which then results in wrong answers. My 'i' variable cant be higher than 65, but it gets up to 86. How could I fix that ? Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
int t1=17, k=3, t=65, s=0, i=0;
while(i < t)
{
    i+=t1;
    cout << i << endl;
    t1+=k;
    s++;
}
cout << s;
 return 0;
}
Nevermind
Last edited on
.
Last edited on
i == 60, then you add t1, which is equal to 26 and so you print out 86
Tried do while loop, but still getting the same results. 'i' is supposed to be less than 65, and 's' is supposed to be 3, but that additional loop screws it all up.
try checking for i + t1 < t
Last edited on
It worked ! Thank you.
Make sure you understand why it works, there is now second implicit loop.
Topic archived. No new replies allowed.