Strange problem in for-loop

M

markus.litz

Hello,

I have a strange problem with some of my c++ code. I have a normal for-
loop like this

for(int i = 1; .....

on MS-Windows with a Microsoft compiler everything is alright, but
when I compile the same source on Linux with gcc-3.4, the variable "i"
is "0" in the first pass of the loop.

Anyone knows the answer of my problem? Maybe a compiler flag?

Thx!
Markus
 
V

Victor Bazarov

I have a strange problem with some of my c++ code. I have a normal
for- loop like this

for(int i = 1; .....

Not sure what's normal about five periods. I've never seen any
program that would actually use that construct.
on MS-Windows with a Microsoft compiler everything is alright, but
when I compile the same source on Linux with gcc-3.4, the variable "i"
is "0" in the first pass of the loop.

Anyone knows the answer of my problem? Maybe a compiler flag?

Maybe. But for the hell of it, I cannot make your program compile
anywhere. My compiler keeps telling me something in line with
"an executable statement outside of any function". I suggest to
look in the FAQ, #5.8.

V
 
M

markus.litz

Not sure what's normal about five periods. I've never seen any
program that would actually use that construct.

I mean a standard for-loop like

for (int i=1; i<10; i++)
{
if(i=9) {...}

}

but "i" start from 0 inside the loop, not from 1 and I don't know why.
This is strange because under windows everything runs fine.
 
T

Thomas Tutone

I mean a standard for-loop like

for (int i=1; i<10; i++)
{
if(i=9) {...}

}

but "i" start from 0 inside the loop, not from 1 and I don't know why.
This is strange because under windows everything runs fine.

Give us a short, self-contained program that demonstrates the error.
Or, please copy and paste the following program, compile and execute
it, and report back on the results:

#include <iostream>

int main()
{
bool flag = false;
for (int i = 1; i<10; ++i) {
if (i==0) flag = true;
}
std::cout << "Markus was "
<< (flag ? "right" : "wrong")
<< '.';
}

Best regards,

Tom
 
S

Stefan

I mean a standard for-loop like

for (int i=1; i<10; i++)
{
if(i=9) {...}

}

but "i" start from 0 inside the loop, not from 1 and I don't know why.
This is strange because under windows everything runs fine.

Hmm, if(i=9) {...} *assigns* the value 9 to i (the result of which
evaluates to true). Are you sure that it shouldn't be "=="?
 
V

Victor Bazarov

Stefan said:
Hmm, if(i=9) {...} *assigns* the value 9 to i (the result of which
evaluates to true). Are you sure that it shouldn't be "=="?

Still, that wouldn't cause the 'i' to "start from 0 inside the loop",
would it? That's why I insist that the OP posts real complete code.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top