Oct 29, 2018 at 7:35pm Oct 29, 2018 at 7:35pm UTC
less is more when it comes to macros. Nothing here needs a macro conditional chain:
(For no good reason I fixed the order of low/med/high to be human-expected)
given the nature of what this IS, a numeric value is probably sufficient?
1 2 3 4 5 6 7 8 9
enum DEBUGLEVEL { LOW,MEDIUM,HIGH, max_dl};
const string levels
#define DEBUGLEVEL HIGH
int main()
{
cout << "debug level is:" << DEBUGLEVEL << endl;
}
or, if you MUST have some text, the old enum to text problem...
1 2 3 4 5 6 7 8 9 10
enum DEBUGLEVEL { LOW,MEDIUM,HIGH,MAXDL};
const string levelz[MAXDL] = {"LOW" ,"MEDIUM" ,"HIGH" };
#define DEBUGLEVEL HIGH
int main()
{
cout << "debug level is:" << levelz[DEBUGLEVEL] << endl;
}
for extra credit take the #define DEBUGLEVEL HIGH out of the code and put it in the project. HIGH won't exist there so you have to use the number.
Last edited on Oct 29, 2018 at 7:42pm Oct 29, 2018 at 7:42pm UTC
Oct 29, 2018 at 11:21pm Oct 29, 2018 at 11:21pm UTC
I took it that the OP wants to do some conditional compilation rather than just change a bit of text.
Oct 31, 2018 at 3:07pm Oct 31, 2018 at 3:07pm UTC
something like this
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 53 54 55 56 57 58 59 60 61 62 63 64
#include<iostream>
#define NONE 0
#define LOW 1
#define MEDIUM 2
#define HIGH 3
#define DEBUGLEVEL HIGH
#if DEBUGLEVEL == NONE
#define ASSERT(x) \
cout << "Debug level NONE\n";
#elif DEBUGLEVEL == LOW
#define ASSERT(x) \
std::cout << "Debug level LOW\n"; \
if (!(x)) \
{ \
std::cout << "Error ! Assert failed ! " <<#x<<"\n"; \
std::cout << "On line "<<__LINE__ << "\n"; \
std::cout << "In file " << __FILE__<< "\n"; \
} \
else \
std::cout << "Success. " << #x << "\n"; \
#elif DEBUGLEVEL == MEDIUM
#define ASSERT(x) \
std::cout << "Debug level MEDIUM\n"; \
if (!(x)) \
{ \
std::cout << "Error ! Assert failed ! " <<#x<<"\n"; \
std::cout << "On line "<<__LINE__ << "\n"; \
std::cout << "In file " << __FILE__<< "\n"; \
} \
else \
std::cout << "Success. " << #x << "\n"; \
#elif DEBUGLEVEL == HIGH
#define ASSERT(x) \
std::cout << "Debug level HIGH\n"; \
if (!(x)) \
{ \
std::cout << "Error ! Assert failed ! " <<#x<<"\n"; \
std::cout << "On line "<<__LINE__ << "\n"; \
std::cout << "In file " << __FILE__<< "\n"; \
} \
else \
std::cout << "Success. " << #x << "\n"; \
#else
std::cout << "Debug OFF.\n" ;
#endif
int main()
{
int x = 5;
std::cout << "First assert.\n" ;
ASSERT (x == 5);
std::cout<<"Second assert\n" ;
ASSERT(x!=5);
std::cout<<"Done.\n" ;
}
Last edited on Oct 31, 2018 at 3:08pm Oct 31, 2018 at 3:08pm UTC
Oct 31, 2018 at 3:18pm Oct 31, 2018 at 3:18pm UTC
Do you still have a question there? Apart from low/med/high being identical apart from their level, it looks good to me, does it work, and if not, what is the issue?