By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

Strange problem in for-loop

P: n/a
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

Nov 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ma*********@googlemail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 14 '07 #2

P: n/a
On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
markus.l...@googlemail.com wrote:
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.
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.

Nov 14 '07 #3

P: n/a
ma*********@googlemail.com wrote:
On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>markus.l...@googlemail.com wrote:
>>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.

I mean a standard for-loop like

[..]
I mean you need to read the FAQ 5.8 and do what it says. For real!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 14 '07 #4

P: n/a
On Nov 14, 12:15 pm, "markus.l...@googlemail.com"
<markus.l...@googlemail.comwrote:
On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
markus.l...@googlemail.com wrote:
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.

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
Nov 14 '07 #5

P: n/a
>>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.

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 "=="?
Nov 15 '07 #6

P: n/a
Stefan wrote:
>>>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.

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 "=="?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.