#define??How do I use it

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

#define??How do I use it

Postby vira-lata » Sat Oct 11, 2003 1:54 pm

I have a lab which is asking to use #define. I don't really know how to do it and my book doesn't cover it a lot since we don't use #define anymore. Can anyone help me to begin this lab? Just a quick tip. Thanks

#define DEPOSIT
#define WITHDRAW
#define CHECKBALANCE
#define EXIT

This program asks for the amount to be deposited/withdraw or check the balance. I have to use functions for the options I have. I'm ok with functions. The problem is the #define's. Thanks again :shock:
vira-lata
 
Posts: 20
Joined: Fri Sep 26, 2003 9:32 pm

Postby twm » Sun Oct 12, 2003 1:48 pm

Example:
Code: Select all
#define DEPOSIT 0
#define WITHDRAW 1
#define CHECKBALANCE 2
#define EXIT 3

#define replaces a string of text with another string of text, it's set up like so:
Code: Select all
#define STRINGTOREPLACE REPLACEWITH

So in the above example, every occurance of DEPOSIT in your code will be replaced with 0 before the code is compiled. Preprocessing takes place before anything else and makes actual text changes in your source file.
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
twm
 
Posts: 22
Joined: Mon Sep 29, 2003 2:38 pm

#define example?

Postby guest » Tue Oct 14, 2003 5:33 pm

can you give me a simple example of how to use the #define?
Something simple so I'll be able to get the idea! Thank you!!!! :roll:
guest
 

Postby Wizard » Tue Oct 14, 2003 9:20 pm

Code: Select all
#include <iostream>
#define you me
using namespace std;

int main() {
int me;
you = 3;
};


Looks like this shouldn't compile. I declare an int called me, then assign an undeclared variable called you. But really, the define replaces you with me at compile time, so it's the same as
Code: Select all
#include <iostream>
using namespace std;

int main() {
int me;
me = 3;
};

User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby raimo » Wed Oct 15, 2003 2:31 am

Code: Select all
#include <iostream>
#define PRINT ;std::cout<<
#define END ;}
#define FUNCTION int
#define PROCEDURE void
#define BEGIN () {
#define CALL ;
#define SANDBOX ()

PROCEDURE foobar
BEGIN
        PRINT "Nonsense\n"
END

FUNCTION main
BEGIN
        PRINT "Hello World!\n"
        CALL foobar SANDBOX
END
User avatar
raimo
 
Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

Postby jgbauman » Wed Oct 15, 2003 2:53 am

@raimo:
That was a very nice exmple of very ugly #define usage ;-)

SCNR
User avatar
jgbauman
 
Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

Postby Guest » Wed Oct 15, 2003 8:08 am

I got the idea!!! THANK YOU GUYS VERY MUCH!
:P :D :P :D :lol:
You are the real teachers!! Teach for love and pleasure
Guest
 

Postby Guest » Thu Oct 16, 2003 11:14 am

jgbauman wrote:@raimo:
That was a very nice exmple of very ugly #define usage ;-)

SCNR


I agree. What would be wrong with a simple example:
Code: Select all
#define TESTVAL 20
int main()
{
    int i;
    for (i = 0; i < TESTVAL; i++)
    {
        printf("i=%d \n", i);
    }
    return 0;
}
Guest
 


Return to For Beginners

Who is online

Users browsing this forum: Google [Bot] and 2 guests