| New Reply |
C++ conditional testing fails, why? |
Share Thread | Thread Tools |
| Feb21-11, 06:19 PM | #1 |
|
|
C++ conditional testing fails, why?
I have a long C++ program that has a few simple statements in it. At the beginning I have ;
#define NUM_DP 10 Further on in the program I have this conditional statement; short int ct, j; . . . ct = op2.test(op2.num); if (ct < (NUM_DP - 1)) for (j = 1; j < 20; j++){ code block } The test function returns a value of ct = 9, why then does the for loop execute . Can't you use constants inside conditional statements? Please help. Thanks. |
| PhysOrg.com |
science news on PhysOrg.com >> Hong Kong launches first electric taxis >> Morocco to harness the wind in energy hunt >> Galaxy's Ring of Fire |
| Feb21-11, 06:30 PM | #2 |
|
|
I don't see why it's running the for loop. Mind you, I would use curly braces for the if statement (around the for loop), but that should work. Can't say without perhaps seeing the entire bit of code. I would suggest you either use a debugger and check the variables right before the if, or add a print in there to see the values right before the if. Something like: Code:
std::cout << "ct = " << ct << "; NUM_DP - 1 = " << (NUM_DP-1) << "; (ct < (NUM_DP-1)) = " << (ct < (NUM_DP-1)) << std::endl; Make sure to #include <iostream> of course.
|
| Feb21-11, 06:43 PM | #3 |
|
|
It is g++ 4.1.2, that I am using, I am just woundering if it is a bug in the version I have for my RISC OS operating system, there is no gdb debugger for my system only a gdbserver which I think requires a remote gdb to work. I will try butting braces around for loop as you suggested. Thanks.
|
| Feb21-11, 06:49 PM | #4 |
|
Mentor
|
C++ conditional testing fails, why?
Since you don't have a debugger, add some output statements as grep suggests, to see the value of ct and (NUM_DP - 1).
|
| Mar7-11, 04:53 AM | #5 |
|
|
You might want to check that you haven't accidentally put a semicolon after the if, like so:
if (ct < (NUM_DP - 1)); as that'll indicate an if with no body. Braces shouldn't make a difference, so unless you're using a hacked-up compiler with a modified parser or funny extensions there's no reason to think that they're the cause of the problem, though stylistically they're a good idea. Also, though unrelated to the problem, you should really avoid macros unless you have some compelling reason to use them. C++ allows you to define well-typed constants. Macros are just direct text substitutions. static short int const numDP = 10; |
| New Reply |
| Thread Tools | |
Similar Threads for: C++ conditional testing fails, why?
|
||||
| Thread | Forum | Replies | ||
| Relativity fails with quantum particles? | Special & General Relativity | 4 | ||
| If GP1 fails to favour GR | Cosmology | 9 | ||
| Integration by Substitution fails | Calculus & Beyond Homework | 9 | ||
| When L'Hopital's Rule fails... | Calculus | 1 | ||
| When Newton Raphson Fails | Calculus & Beyond Homework | 4 | ||