C++,Macro Problem

I've written below simple code.Actually need to do that the value which is given by an user that will use as macro and macro value will automatically initialyzed to all needed place.Not to given every time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include "conio.h"
#define AB def()

int main(int argc, char* argv)
{
int a=3,b=2;
int def();
int add(int,int);
int sub();
std::cout << "\nAddition Result :" << add(a,b);
std::cout << "\nSubtraction Result :" << sub();
_getch();
//std::cin.get();
//system("pause");
return 0;
}

int def()
{
	int m;
	std::cout << "\nEnter The Value Of Def :";
	std::cin >> m;
	return m;
}

int add(int x,int y)
{
 return (AB+x+y);

}

int sub()
{int v;
	std::cout << "\nEnter The Value Of v :";
	std::cin >> v;
	int n=(AB-v);
 return (n);

}
So please any C/C++ expert please help me out from this problem.Thnks a lot
Last edited on
Functions can't be defined in main, so move line 8 through 10 out of main.
I've done what u suggest but it can't work which I need.please help me Warnis my friend.
I want once I've giving the value of def which keep stored in m.Second time the value of m automatically taken by int sub() function.Once I given the value of def,I don't want to give it again through the program.
You have to define them BEFORE you use int main( ). So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int add( int x, int y );
int sub( );
int def( );

int main( ){
    ...
}

int def()
{
	int m;
	std::cout << "\nEnter The Value Of Def :";
	std::cin >> m;
	return m;
}

etc.
Okay, I see what you mean.

Macros don't work that way. They are a text replace feature that takes place before compilation.
For example, add() could be rewritten like this and it wouldn't change anything:
return def()+x+y;

If you want def() to be called only once, you'll to store the value in a variable and pass around the functions.

EDIT: Oh, and functions can be declared inside other functions, which is what you're doing. In fact, one of the most weird behaviors of C++ is because of that.
Last edited on
Hi NGen,thanks for your kind touch on my problem.Like today u pls keep touch with me my friend.
Hi my friends helios,I mostly appreciate with your suggestion.I agree Macros don't work that way.yes I've solved this problem by argument passing of the functions.It just like you said.Thank you for your suggestion.So,my friend pls keep touch with me on future.
Topic archived. No new replies allowed.