Easily comment out cout

In my code, i have lots of couts which i use for debugging purposes. Rather than having to comment out all the cout statements, i've heard there is a much easier way to get the compiler to ignore any cout statements it comes across by using things such as #ifdef........ however, i really don't know how to implement this.

Let's take a really simple program:
1
2
3
4
5
6
7
8
9
10
11
12
13

int main()
{
   float x, y; 
   x = 5.3;
   y = x * x;

   cout << "x^2 = " << y << endl;

   return 0; 

}


so how would i implement the #ifdef thing (if indeed you can) on to something such as this?
Well you could do this:
1
2
3
4
5
#ifdef USEDEBUG
#define Debug(x) std::cout << x
#else
#define Debug(x) 
#endif 


Then you would write your cout line like this:
Debug("x^2 = " << y << endl);

If the macro USEDEBUG is defined, this will resolve to:
std::cout << "x^2 = " << y << endl;
and otherwise, it will just disappear, since Debug(x) will be defined to to nothing.

Hope this helps.
Works a treat, just what i needed. Thanks. :)
Xander314, do you know of a way to do this with functions and not include the return value?

Here's the code I just wrote.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//includes
#include "stdafx.h"
#include <iostream>
#include <vector>

//function prototypes
int outputVec( vector v );

//Debug
#define USEDEBUG

#ifdef USEDEBUG
#define Debug( x ) std::cout << x
#else
#define Debug( x ) 
#endif

//typedef
typedef std::vector< int > vector;
typedef std::vector< int >::iterator iterator;



int _tmain(int argc, _TCHAR* argv[])
{
	Debug( "Debug is on.\n" );

	vector v;

	for( int i = 0; i < 10; ++i )
		v.push_back( i + 2 );

	Debug( outputVec( v ) );

	return 0;
}



int outputVec( vector v )
{
	iterator iter = v.begin();

	while( iter != v.end() )
	{
		std::cout << *iter << '\n';
		++iter;
	}

	return 1;
}


I tried this with:
void outputVec( vector v )

But with this, I am unable to compile. So by using int as a return value, it compiles and runs fine, only I print out the return value along with the vector.

Cheers for any help.
When you change outputVec() to void on Line 40, you must also change the prototype on Line 7 to return void.
Yeah, I did that. But it wouldn't except void for the Debug( x ) std::cout << x
So this:
Debug( outputVec( v ) );
caused an error when 'outputVec' is a void function? This is because 'outputVec' doesn't return a value, so 'outputVec(v)' has no value to be send to std::cout.
Actually, if you intend to use Line 33, you need to write operator<<() for your vector and replace Line 33 with Debug( v ); instead.
It's fixed now.

I added another #define :
#define DebugVec( v ) outputVec( v )

And changed line 33 to:
DebugVec( v );

Thanks for the help!
Rather than make a new thread, can someone tell me if this is possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	bool dbug = true;
	char key = ' ';

	std::cout << "Do you wish to debug?\nPress Y or N.\n";

	while( ( key != 'Y' ) || ( key != 'y' ) || 
			( key!= 'N' ) || ( key != 'n' ) )
	{
		while( ! _kbhit() )
		{ /*do nothing*/  }

		getKeyPressed( &key );

		if( ( key == 'Y' ) || ( key == 'y' ) || 
			( key == 'N' ) || ( key == 'n' ) )
			break;

		std::cout << "Invalid input. Please press either Y or N.\n";
	}

	switch( key )
	{
		case 'Y':
			dbug = true;
			break;

		case 'y':
			dbug = true;
			break;

		case 'N':
			dbug = false;
			break;

		case 'n':
			dbug = false;
			break;
	}

	/*From here onwards*/
	if( dbug )
	{
		#define Debug( x ) std::cout << x
		#define DebugVec( v ) outputVec( v )
		std::cout << "dbug active.\n";
	}
	else
	{
		#define Debug( x )
		#define DebugVec( v )
		std::cout << "dbug not active.\n";
	}


Or do I have to actually define these at compile time?

While debugging this and making dbug = true. Stepping into the if statement on line 41, the only line that gets executed is line 45. Lines 43 and 44 are skipped.
Last edited on
No. #define has no concept of scope; they are all ran before the program is compiled or run.
If you want to control the debugging statements with a boolean variable at runtime, why not just do this:
1
2
if (dbug)
  std::cout << "debug message" << std::endl;

or whatever other debugging code you want?
Thanks firedraco.

And Xander, I just like to have less code.
Then how about:
#define Debug(x) if (dbug) x;

Then you would do:
Debug("debug message")
to do output conditional on the variable dbug.
Topic archived. No new replies allowed.