For and if loop problem

I want this code to execute so I get

This
is
working

and it seems pretty straight forward to me. When it runs, however, I'm caught in an infinite loop of "is". The weirdest part to me is that the i-increment should clearly start at 0, yet I never get into the "This" statement. Is my problem in the for loop setup or the entry to the if loops?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()
{
	for (int i=0; i<3; i++)
	{	
		if(i=0)
		{
			cout << "This" << endl;
		}
		else if (i=1)
		{
			cout << "is" << endl;
		}
		else
		{
			cout << "working!" << endl;
		}
	}	
getchar();
}
if(i=0) should be if(i==0), same thing for the else if part. What you're doing now isn't comparing a value to i, you're assigning a value to i. Huge difference.
Thanks a ton! It's been killing me! Yea, as stupid as it sounds, I always forget to use logical for if statements.
Yea, as stupid as it sounds, I always forget to use logical for if statements

Then it would behoove you to turn up your warning levels and pay attention to warnings generated when you compile.
You are essentially asking if (i can be assigned the value of 1); this statement will always be true.
Topic archived. No new replies allowed.