Help with for loop

Hey Everyone,

I'm new to this forum and I have a problem I can't seem to find the answer to anywhere. I have the following loop in a program for school and when I run it I get infinite outputs. The conditions for the internal if structures don't seem to be working correctly. Any help would be appreciated. The output looks like this:

test2
test
here3
test2
test
here3
test2
test
here3
test2
test
here3
test2
test
here3
test2
test
here3
test2
test
here3
test2
test
here3
test2...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  for (int i = 0; i < length; i++)
	{
		cout << "test";
		if (i = 0)
		{
			sum = decArray[i];
			cout << "here1";
		}
		else if (decArray[i] > decArray[prevNumb])
		{
			sum = decArray[i] - decArray[prevNumb] + sum;
			prevNumb++;
			cout << "here2";
		}
		else
		{
			sum = decArray[i] + sum;
			cout << "here3";
		}
		cout << "test2";
	}
line 4 - I think you mean to use the equality operator ==, not the assignment operator =

How is length defined? What about prevNumb?
Last edited on
Oh wow thank you. I can't believe that's what was causing it. length is assigned from the length of a vector and prevNumb is instantiated outside of the loop. changing the condition to == did the trick.
Topic archived. No new replies allowed.