Archived

This topic is now archived and is closed to further replies.

CmdJoeM

How do I use a If statement in a Macro?

Recommended Posts

I am currently taking courses at a college in Adv. C++ Programming. My professor wants me to use a Macro to calculate when the hours worked is greater than fourty then return 0 or 1 to continue on with the code. I can calclate using macros but as soon as i put the if statement in I get five errors realting to the if statement in my main.cpp. Summary of Code: main.cpp********************** if(MACRO(hw)>0) { Calculate pay. } macro.h #define MACRO(hw)(if(hw>40){return 1;});

Share this post


Link to post
Share on other sites
quote:
#define MACRO(hw)(if(hw>40){return 1;});

Macros don''t "return values"...they simply replace the word MACRO with whatever it is defined as. If the macro has parameters, it first replaces those parameters in the macro, then replaces the whole macro.

The way to do this is:

#define MACRO(hw) hw > 40 ? 1 : 0

Share this post


Link to post
Share on other sites
Right now my Macro looks like:

#define HOURS(hw) ((if(hw>40){return 1;}));

and my main error is:

syntax error: ''if''

when i double click on it it takes me to the if in my main:

if(HOURS(hoursworked)
{ //This uses a macro when the hours are greater than forty
cout< cout<}

My other errors after the syntax are related to ) before if but i am sure tat they will go away after i fix this problem

Share this post


Link to post
Share on other sites
Your first one worked

#define HOURS(hw) hw > 40 ? 1 : 0

Would you please tell me what the ?1:0 is all about is it if true return 1 and else return 0?

Share this post


Link to post
Share on other sites
quote:
Original post by CmdJoeM
Would you please tell me what the ?1:0 is all about is it if true return 1 and else return 0?


Yes. The ?: operator first evaluates the expression on the left side of "?". If it is true, it evaluates the expression to the left of the ":". If the original expression was false, it instead evaluates the expression to the right of the ":".

So yes, if I put (condition) ? 1 : 0, it will return 1 if the condition is true, and 0 if it is false.

You can also put more complex expressions to the right of the "?". For example, to prevent a divide by zero, I can do this:

b == 0 ? a / (b + 1) : a / b;

Meaning that if b is zero, we add 1 to b before dividing so we don''t get an error. But if b != 0, we can simply divide a / b.





Share this post


Link to post
Share on other sites
You got it.

It''s short hand for ''if -conditional- then -statement- else -statement-''. -conditional-?true:false. It''s great for imbedding simple conditionals into statments, as you can plug ?: into nearly anything.

For instance; you can do function(? but not function( if() ).

Share this post


Link to post
Share on other sites
quote:
Original post by Deyja
You got it.

It''s short hand for ''if -conditional- then -statement- else -statement-''. -conditional-?true:false.

No it isn''t, as it''s an expression, not a statement. They have a value.

Share this post


Link to post
Share on other sites
There''s no need to use the ternary operator in that case.

#define MACRO(hw)( hw > 40 ? 1 : 0 )

Is equivalent to

#define MACRO(hw)( hw > 40 )

So you''d use:

if( MACRO(hw) )

Share this post


Link to post
Share on other sites
ask your professor whether you can use a function instead

macros in c++ should only be used for things that you can''t do another way. really, really, really a last resort.

it''s good to learn the features of the programming language available to you.

i hope you get told ''now you know how to do this, you should never do this.''

Share this post


Link to post
Share on other sites