#define and getch();
Hey all, I''ve seen 3 things used recently in source code, all of which I don''t understand. And would like some explanations to what they are, and what they do. One being #define, another being getch(); and the finaly being \t and \n, I''ve seen them used in cout lines, and seems to have something to do with centering text, which I''m hoping for, because I''m making a text-based console game, and I want to know how to center my text, thanks!
~ Solid Metics
"You probably use those fat crayons made for retarded kids." - warbux
"You probably use those fat crayons made for retarded kids." - warbux
#define: read the chapter in your C++ book about preprocessor directives.
getch: a system-specific extension which allows you to read a single character from the keyboard.
\t: the tab character.
\n: the newline character.
How to center text: get the length of the string and the width of the screen (usually 80). indent the text by ((screenwidth - stringlength) / 2).
Don''t listen to me. I''ve had too much coffee.
getch: a system-specific extension which allows you to read a single character from the keyboard.
\t: the tab character.
\n: the newline character.
How to center text: get the length of the string and the width of the screen (usually 80). indent the text by ((screenwidth - stringlength) / 2).
Don''t listen to me. I''ve had too much coffee.
listen to #define you use it to "define" things it is used different in headers but in programs is used as such
#define thing_you_want_to_define definition
EXAMPLE:
#define BACKGROUND_BLACK 0
by the way in consoles the colors gor foreground and background are important to make cool looking games. 0 is the definition in console colors 0=black
shuv-it
#define thing_you_want_to_define definition
EXAMPLE:
#define BACKGROUND_BLACK 0
by the way in consoles the colors gor foreground and background are important to make cool looking games. 0 is the definition in console colors 0=black
shuv-it
50/50 Robotics and Software
an #define is used by the preprocessor. The preprocessor is an part of the compiler wich handles all # commands. If you use an #define as this:
When you compile this the preprocessor kicks in before the compiler compiles your code and replaces ALL occurrences of MAX_ITEMS in the code with 10. In fact, if you look at the precompiled code you see that the new code looks like this:
The define is gone.
I hope this helps,
Sand Hawk
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
#define MAX_ITEMS 10int Array[MAX_ITEMS];
When you compile this the preprocessor kicks in before the compiler compiles your code and replaces ALL occurrences of MAX_ITEMS in the code with 10. In fact, if you look at the precompiled code you see that the new code looks like this:
int Array[10];
The define is gone.
I hope this helps,
Sand Hawk
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
If you want to get advanced, you can #define terms to things more complicated than simple numbers - this is frowned upon by some people since it can make reading your source code much harder. In fact, one of the tricks used in the obfuscated C code contest is to #define things to make the code less readable.
The website is well worth a look - if only to see how not to write code!
One particularly cool use of #define I saw once was to #define a single character in such a way that, by making a filled circle of that character (alternating with another symbol I think), the program effectively measured the area and height of that circle, and used the two values to calculate Pi (probably with another line of code to output). By tweaking the shape of the code, you could get 22/7 (the best approximation without going to 3 digits) quite easily.
The website is well worth a look - if only to see how not to write code!
One particularly cool use of #define I saw once was to #define a single character in such a way that, by making a filled circle of that character (alternating with another symbol I think), the program effectively measured the area and height of that circle, and used the two values to calculate Pi (probably with another line of code to output). By tweaking the shape of the code, you could get 22/7 (the best approximation without going to 3 digits) quite easily.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement