C/C++ C++ conditional testing fails, why?

AI Thread Summary
The discussion centers on a C++ program where a conditional statement is unexpectedly allowing a for loop to execute. The program defines a constant `NUM_DP` as 10 and checks if a variable `ct`, which is set to 9, is less than `NUM_DP - 1` (which equals 9). The confusion arises as the expectation is that the loop should not execute since 9 is not less than 9. Participants suggest using debugging techniques, such as printing variable values before the conditional check, to clarify the situation. They also recommend ensuring there are no accidental semicolons after the if statement, which would lead to an empty body. Additionally, there is a caution against using macros for constants, advocating instead for typed constants in C++. The conversation highlights the importance of careful syntax and debugging in C++ programming.
John O' Meara
Messages
325
Reaction score
0
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.
 
Technology news on Phys.org
John O' Meara said:
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.
Constants are fine in expressions. There are pitfalls for certain defines (side effects and such), but that's not relevant here.

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;

You should be able to see right away if the values are other than you think. Note that I just whipped out that line. I think it should be right, but I never assume that until it's compiled and tested. :smile: Make sure to #include <iostream> of course.
 
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.
 
Since you don't have a debugger, add some output statements as grep suggests, to see the value of ct and (NUM_DP - 1).
 
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;
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top