Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange problem in for-loop

Reply
Thread Tools

Strange problem in for-loop

 
 
markus.litz@googlemail.com
Guest
Posts: n/a
 
      11-14-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-14-2007
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


 
Reply With Quote
 
 
 
 
markus.litz@googlemail.com
Guest
Posts: n/a
 
      11-14-2007
On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-14-2007
wrote:
> On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> 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


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 12:15 pm, "markus.l...@googlemail.com"
<markus.l...@googlemail.com> wrote:
> On 14 Nov., 17:56, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>
> > 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


 
Reply With Quote
 
Stefan
Guest
Posts: n/a
 
      11-15-2007

>>> 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 "=="?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-15-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
strange problem John Wireless Networking 4 02-14-2005 05:40 PM
Problem installing modules on redhat 9 - strange connection problem championsleeper Perl Misc 0 01-26-2005 12:19 PM
Strange error! Compiler problem? Or code problem?!!!!!!! Pedro Miguel Carvalho C++ 2 10-25-2004 02:06 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57