How do I use a If statement in a Macro?
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;});
Hi Joe,
Could you post the five errors you are getting? That would make it easier for people to help you
Thanks,
Michael
Could you post the five errors you are getting? That would make it easier for people to help you
Thanks,
Michael
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
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
#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
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?
#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?
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.
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() ).
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() ).
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.
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) )
#define MACRO(hw)( hw > 40 ? 1 : 0 )
Is equivalent to
#define MACRO(hw)( hw > 40 )
So you''d use:
if( MACRO(hw) )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement