C++ conditional testing fails, why?

In summary: NUM_DP 10In summary, the conditional statement in the program is checking to see if the value of ct is less than the number of DPs defined, and if it is, the for loop executes.
  • #1
John O' Meara
330
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
  • #2
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.
 
  • #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.
 
  • #4
Since you don't have a debugger, add some output statements as grep suggests, to see the value of ct and (NUM_DP - 1).
 
  • #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;
 

Related to C++ conditional testing fails, why?

What is conditional testing in C++?

Conditional testing in C++ refers to the process of using conditional statements, such as if/else or switch, to evaluate a specific condition and execute different blocks of code depending on the result of the evaluation.

Why does conditional testing fail in C++?

Conditional testing may fail in C++ due to a variety of reasons, such as incorrect syntax, logical errors in the code, or unexpected input data. It is important to carefully review the code and debug any issues to ensure the conditional testing functions correctly.

How can I troubleshoot a failed conditional test in C++?

To troubleshoot a failed conditional test in C++, you can use debugging tools, such as breakpoints, to step through the code and identify any errors. You can also use print statements to output the values of variables and see if they match your expected results.

Can I use multiple conditions in a single conditional test in C++?

Yes, you can use multiple conditions in a single conditional test in C++. This can be done using logical operators, such as && (and) and || (or), to combine multiple conditions and evaluate them together.

What are some common mistakes to avoid when using conditional testing in C++?

Some common mistakes to avoid when using conditional testing in C++ include forgetting to include a default case in a switch statement, using the assignment operator (=) instead of the equality operator (==), and not properly handling unexpected input data, leading to unexpected results.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
958
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
4
Views
644
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
22
Views
818
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top