for loop problems

Hello everyone. I am new to the whole coding and programming scene and currently learning. This is the code.


#include "stdafx.h";
#include <cstdlib>;
#include <iostream>;
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
int nLoopCount;
cout << "Enter loop count: ";
cin >> nLoopCount;
for(int i = 1; i <= nLoopCount; i++)
{
if(i = 1)
{
cout << "We have finished " << i
<< " loop" << endl;
}
else
{
cout << "We have finished " << i
<< " loops" << endl;
}
}
system("PAUSE");
return 0;
}

Now every time I run the program it repeats "We have completed 1 loop.". To me It seems the i is not being incremented as I wanted to do. Will someone please tell me a fix for this issue? Or is a for loop unable to run it statements?
Last edited on
Does it do this for any value of nLoopCount?
Also the condition of your if statement should use ==
Yes. For any value I put in for nLoopCount it does the same thing. The example I was using did not use the if statement. It only used the else portion of the if statement. When i changed the code to that of the example (without the if statement) It worked fine. Ok and I will try to compare the values and see what happens.
Last edited on
Thank you by the way. After I changed the if statement to i == 1 it worked perfectly. Thank you again.
Topic archived. No new replies allowed.