For loop horror
-
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh:A classic C/C++ error; you haven't been coding long enough if you haven't run into this one before.
Steve
-
The compiler will help you if you are good enough to use the loop index in the loop body... :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
Luc Pattyn wrote:
The compiler will help you
Only the more recent ones - VC6 for example promoted the loop index to scope outside the loop.:)
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh:Ooh, that's a good one. I've had it before as well, and even now only after reading the first comment did I get it. I was going to say, it's not really a horror because we don't know what MIN_VAL and MAX_VAL are. :doh:
MY NEW BLOG
-
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh: -
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh: -
Llasus wrote:
Maybe everyone has these kind of moments.
I had that exact moment about 15 years ago in a C++ app. It took me half the day trying to figure out why the loop executed once and only once. Marc
Me, too. I think it's a rite of passage for C programmers. :doh:
Christopher Duncan Author of The Career Programmer and Unite the Tribes Coming soon: Got a career question? Ask the Attack Chihuahua! www.PracticalUSA.com
-
Me, too. I think it's a rite of passage for C programmers. :doh:
Christopher Duncan Author of The Career Programmer and Unite the Tribes Coming soon: Got a career question? Ask the Attack Chihuahua! www.PracticalUSA.com
-
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh:Switch to a language that does not have loops (Haskell, for instance) and you won't have these problems any more :)
-
Luc Pattyn wrote:
The compiler will help you
Only the more recent ones - VC6 for example promoted the loop index to scope outside the loop.:)
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
cp9876 wrote:
VC6 for example promoted the loop index to scope outside the loop.
That is one of many reasons why VC6 is regarded as extremely braindead... In fact it isn't a C++ compiler. Just a compiler that compiles a language looking a lot like C++...
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh:Oh my god....and I was looking ....and looking....and looking.... The conditional statement was fine....and I couldn't find it at all....keep looking and reading all the comments here. Couldn't find it. I was about to give up and ask when BLAM!!!!! I saw it...there... That never happened to me. Luckily! I've done similar though and lost a good 3~4 hours of time trying to figure it out. I am actually surprised that this compiles though...and runs. Shouldn't the compiler complain about this?
Dewm Solo - Managed C++ Developer
-
I just spent an hour trying to think why the process didn't work.
for(int nindex = MIN_VAL; nindex <= MAX_VAL; nindex++); { //50 lines of code that processes something }
Maybe everyone has these kind of moments. :doh:Dammit, I had to stick this in the IDE and debug it to work out what the hell was wrong with this. I think I've been doing a little too much ASP maintenance today :sigh:
-
if it makes you feel any better, it took me about 5 mins of looking at it over and over again before i noticed what you were talking about :doh:
~Nitron.
ññòòïðïðB A
startsame here
-
*beeeep* what's wrong...can't say "semicolon" ???
caridad
-
Oh my god....and I was looking ....and looking....and looking.... The conditional statement was fine....and I couldn't find it at all....keep looking and reading all the comments here. Couldn't find it. I was about to give up and ask when BLAM!!!!! I saw it...there... That never happened to me. Luckily! I've done similar though and lost a good 3~4 hours of time trying to figure it out. I am actually surprised that this compiles though...and runs. Shouldn't the compiler complain about this?
Dewm Solo - Managed C++ Developer
Dewm Solo wrote:
Shouldn't the compiler complain about this?
No, it's perfectly valid to stuff all of the loops processing into the
( ; ; )
of afor
statement and create a loop with no explicitly defined code block. This has it's uses, but I always just enjoyed using it to obfuscate my code :) -
if it makes you feel any better, it took me about 5 mins of looking at it over and over again before i noticed what you were talking about :doh:
~Nitron.
ññòòïðïðB A
start -
A classic C/C++ error; you haven't been coding long enough if you haven't run into this one before.
Steve
-
if it makes you feel any better, it took me about 5 mins of looking at it over and over again before i noticed what you were talking about :doh:
~Nitron.
ññòòïðïðB A
startI didn't get it until Marc's comment about the body only running once.
This blanket smells like ham
-
Oh my god....and I was looking ....and looking....and looking.... The conditional statement was fine....and I couldn't find it at all....keep looking and reading all the comments here. Couldn't find it. I was about to give up and ask when BLAM!!!!! I saw it...there... That never happened to me. Luckily! I've done similar though and lost a good 3~4 hours of time trying to figure it out. I am actually surprised that this compiles though...and runs. Shouldn't the compiler complain about this?
Dewm Solo - Managed C++ Developer
Dewm Solo wrote:
I am actually surprised that this compiles though...and runs. Shouldn't the compiler complain about this?
Yeah. While it should of course compile, I'd want this warning: http://msdn2.microsoft.com/en-us/library/9x19t380(vs.80).aspx[^]
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
if it makes you feel any better, it took me about 5 mins of looking at it over and over again before i noticed what you were talking about :doh:
~Nitron.
ññòòïðïðB A
startI had to look for help :)
xacc.ide
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."