I have been looking at this code for 20min and I can't understand why this loop is infinite, it should print 30 elements.
Some text, because post is mostly code.
I have been looking at this code for 20min and I can't understand why this loop is infinite, it should print 30 elements.
Some text, because post is mostly code.
This is a common mistake when programming in C(++), early on, people mistake the equality operator ==
with the assignment operator =
.
What you're doing here with the line if (i = 1){
is setting i
to 1 on each iteration of the loop, so i never gets any larger.
To fix it, simply change that line to if (i == 1) {
I'm guessing it's because if (i = 1)
assigns 1
toi
. You meant to compareif (i == 1)
the if statement is wrong. it should be
if(i == 1)
currently on each loop you are assigning i back to 1. Hence the reason why the loop is infinite