🔒 Locked
using #defined values
Advertisement
Original Post
can anybody tell me why I cant divide by a #defined value?
ex-
This C++ syntax doesnt work:
#define X 5
long Y = 3;
long Z = Y/X;
but this does:
#define X 5
long Y = 3;
long Z = Y/(X+0);
Thanks,
Ben
That are both valid examples. What do you mean by "doesn''t work"?


You shouldn''t be surprised that both of those set Z equal to 0. It''s a result of truncation. If that''s not the trouble you''re having, then I''m sorry if I''ve underminded your intelligence.
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
I mean that it gives me a syntax error on the character after the #defined value, my actualy code is:
#define LoadStepNum
...
long LoadBarProgress = 0;
...
LoadBarProgress += screenwidth / (LoadStepNum + 0);
without the () and the + 0 it gives me a syntax error on the '';''
this doesnt make any sense, so thanks for the help
-Ben
#define LoadStepNum
...
long LoadBarProgress = 0;
...
LoadBarProgress += screenwidth / (LoadStepNum + 0);
without the () and the + 0 it gives me a syntax error on the '';''
this doesnt make any sense, so thanks for the help
-Ben
Only thing I notice is that you don''t have anything defined for LoadStepNum. Make sure there''s a number in there, and make sure that you''ve got it written as:
#define LoadStepNum 7
No equal sign or anything...
Although I''m betting you''ve already got it like this and it was simply a copying error.
#define LoadStepNum 7
No equal sign or anything...
Although I''m betting you''ve already got it like this and it was simply a copying error.
wow do I feel stupid, I was sure I typed a 4 there.
ack
I think I will go hide until this message is forgotten.
ack
I think I will go hide until this message is forgotten.
lol... bump 
Well I have done similer things... note similer, but in no way was it near this stupid!!
CEO Plunder Studios
Well I have done similer things... note similer, but in no way was it near this stupid!!
CEO Plunder Studios
I wasnt getting any, just the syntax errors. not very intelegent error message, but not a very intelegent thing to be doing so oh well
[edited by - oveja-carnivora on June 2, 2002 3:14:27 PM]
quote:
Original post by ZealousElixir
Heh. How did you not notice the "divide-by-zero" errors you were getting?
[edited by - oveja-carnivora on June 2, 2002 3:14:27 PM]
LoadBarProgress += screenwidth / (LoadStepNum + 0);
Without LoadStepNum defined, that would appear to the compiler as
LoadBarProgress += screenwidth / (+ 0);
and furthermore
LoadBarProgress += screenwidth / 0;
So, if you ran that code, how did you avoid a process-fatal error?
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
Without LoadStepNum defined, that would appear to the compiler as
LoadBarProgress += screenwidth / (+ 0);
and furthermore
LoadBarProgress += screenwidth / 0;
So, if you ran that code, how did you avoid a process-fatal error?
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
That actualy never ran, it was a one of a bunch of errors that I was working thru. just trying to fix compiler errors
I see.
"It takes a big man to cry, but it takes a bigger man to laugh at that man."
Classy it weren''t, but boys will be boys. I doubt seriously that your repremand, no matter how appropriate, will have the intended effect. Besides, he took our time, we take his pride. It would be fair enough to stop there, but we also gave him the ability to recognize stupid errors in the future.
No harm intended.
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
Classy it weren''t, but boys will be boys. I doubt seriously that your repremand, no matter how appropriate, will have the intended effect. Besides, he took our time, we take his pride. It would be fair enough to stop there, but we also gave him the ability to recognize stupid errors in the future.
No harm intended.
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
would using a const long instead of a #define be better? I''m prob wrong so ignor me if I am. Its just i read somewhere that its ment to be ''better practice'' to use const''s and not #define''s in C++. I still don''t completely follow that tho.
-J
-J
Macros are evil for three reasons. See the C++ FAQ Lite for more info. For single values, const variables are more desirable, but they require you do use extern and to define the const in a single translation unit, whereas #define can be simply tossed into a
Wait a minute. const values DON'T require you to use extern, do they?
Never mind.
Anyway, macros are evil, and yes, in this situation, it would have been better to use const (certainly would have eliminated the need for this thread...
)
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
EDITED for schtuff.
[edited by - zealouselixir on June 2, 2002 7:48:53 PM]
Wait a minute. const values DON'T require you to use extern, do they?
Never mind.
Anyway, macros are evil, and yes, in this situation, it would have been better to use const (certainly would have eliminated the need for this thread...
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
EDITED for schtuff.
[edited by - zealouselixir on June 2, 2002 7:48:53 PM]
Macros are only evil when used to replace inline functions, constants or typedefs
My project is frying my brain...I didn''t get that for 5 seconds...
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement