Hi guys,

Can I enable/disable an #ifdef based on a specific condition?

Let's say that I've a loop

#define MYCONDITION 1
// code here
for (int i=0; i<5; ++i)
{
   printf("Hello #%d", i);
#if MYCONDITION
   printf("I wanna be reached only if i == 3, is that possible?");
#endif
}

Is that possible?

Thank you for your help.

Can I enable/disable an #ifdef based on a specific condition?

Yes, but only if the condition is available at compile time. In your case the value of i is a run time quantity, so you cannot use it with the preprocessor. I think what you really want is an if statement:

for (int i=0; i<5; ++i)
{
   printf("Hello #%d", i);

   if (i == 3)
   {
      printf("I wanna be reached only if i == 3, is that possible?");
   }
}

Thank you for your answer.
In my example, MYCONDITION is known. But in my code, MYCONDITION is not. It's known only at the time of execution as an argument in the command line.

Thank you for your answer.
In my example, MYCONDITION is known. But in my code, MYCONDITION is not. It's known only at the time of execution as an argument in the command line.

Then what you need to do is parse the command line and store the relevant arguments in variables and then use those variables to determine what happens in your code.

So perhaps something like this?

#include<iostream>
using namespace std;

#define MAXVAL 10

// program takes one argument and is invoked like this:
// program.exe {value for myCondition 1-10}
// e.g.
// program 2
// This will run the program and will display an extra message on the 2nd iteration of the loop
int main(int argc, char* argv[])
{
	int myCondition;

	// Check the command line parameters...
	// First ensure that there are only two arguments (filename and the value for "myCondition")
	if(argc==2)
	{
		// ok, we have the correct number of arguments, 
		// so convert the 2nd argument in the array to an int
		// and assign it to myCondition.
		myCondition = atoi(argv[1]);

		// Ensure myCondition is within our bounds
		if(myCondition<=MAXVAL && myCondition>0)
		{
			// now we'll loop 10 times and say hello each time
			for(int i=1; i<=MAXVAL; ++i)
			{
				cout << "Hello #" << i << endl;
				
				if( i==myCondition) // display an extra message or three!
				{
					cout << "i = " << i << " and myCondition = " << myCondition << endl;
					cout << "This block of code only gets called when i == myCondition." << endl;
					cout << "Is that what you were aiming for?" << endl << endl;
				}
			}
			return 0; // success!
		}
		else 
		{
			// value of myCondition is out of bounds
			cout << "ERROR: the value of the argument must be 1..10";
			return 1; // failed
		}
	}
	// if we got here, then the program was passed the incorrect number of arguments
	cout << "ERROR: program takes one argument!";
	return 1; // failed
}

Apologies for the extra long lines in the code above...it'll look neater if you copy and paste the above code into a text editor!

Anyway, the above program takes one parameter (an int value between 1 and 10). As long as it gets a valid parameter, it loops 10 times and prints hello. But whenever the value of i in the loop is equal to the value of the parameter, an extra block of messages are displayed.

Attached is a .png of the program running...I've very imaginatively called the program program.exe.

By looking at the .png you can see that it traps errors with the command-line parameters..I've tried running it a few times passing it bad parameter values before finally passing a valid parameter of 6.

Anyway, I hope that is close to the kind of thing that you are trying to achieve.
It's probably not exactly what you're after, but it at least shows you how to deal with command line arguments inside your program.

Cheers for now,
Jas.

Alternatively, if you only wanted the extra block to be shown if the value of the parameter was 3 then you could change this block from my previous listing:

if(i==myCondition)
{
	cout << "i = " << i << " and myCondition = " << myCondition << endl;
	cout << "This block of code only gets called when i == myCondition." << endl;
	cout << "Is that what you were aiming for?" << endl << endl;
}

to this:

if(myCondition==3)
{
	cout << "This only gets called when myCondition is passed in as 3" << endl;
}

And then your results will look like the attached .png
So now the extra block will appear on each iteration, but only if the value of the passed in parameter is 3

Thank you JasonHippy.
I was thinking about this solution, but I thought there's another way more elegant to solve this problem.

I need to create some file based on the command line args and after that write some information into those files. By applying your suggestion, I think I can do it.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.