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.

#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.Thanks a lot

Recommended Answers

All 7 Replies

So, what are your problems. Are we supposed to copy your code into our compilers, compile it and then check for errors. (By the way, I just did all this and found that your program ran smoothly giving no errors).

>So,please any C/C++ expert please help me out from this problem
what problem? your program is working fine

Note: Use of macros are not recommended for C++ programmers. There is no good reason to practice it. Read section 3.1 of this document to know why.
Also note that conio.h is a non-portable file and is thus, not encouraged. Read the section 2.3.2 of the same document.

I am writing a very big program and this macro concept is needed to use there.This program is only to clear my doubt.Actually I've mensioned earlier what I need to do,it might be possible you ignored that but its ok my friend.Let u know that 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.

>I am writing a very big program and this macro concept is needed to use there.
Macros are evil: either it be a small program or a big one. The risk of complexity increases as the LOC (Line Of COde) increases. If your program is big, there are even more reasons to hate macros.
>Actually I've mensioned earlier what I need to do,it might be possible you
>ignored that but its ok my friend

If you think that you told me, perhaps you need to be more expressive. Here is a very nice article on How to ask question the smart way. You are expected to read this, it will help.
>value of def
there is no value of def. def is a function. The correct phrase would be
"the value returned by def"

>Once I given the value of def,I don't want to give it again through the program.
You can pass the value of m as a argument to the function sub(). This would be perhaps the most nice way to do this.

Thanks for Your quotes Siddhant,As I am not getting to solve this program(using macro)thatswhy I need someone help.Yes said right that passing the value of m as a argument to the function sub() and I've done using this concept and really your article is very nice.

I am glad I could help you.
Please mark the thread as "Solved" by clicking the "Mark this thread Solved" link. It would be near the bottom of this page.

No the problem yet not to solved as I need it would be solve by using macro.If u help be to solve this I'll thankfull to u.

Hi all my friends,I've solved my problem.now I have understand how to solved this type of problem using Macros.It is some increase my knowledge.
Thanks to all for yours support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.