Very simple beginner problem on finding area of circle

Hello, I am new to the forums and C++ in general which I started learning in January. I am on a problem that seems extremely simple to me, but I cannot understand what I am doing wrong. Clearly this is a homework question..and I am not asking for a direct answer, only a hint because I feel like i'm on the verge of finding answer but have searched the web to no avail.

Here are the instructions:
Write program that defines macro called AREA to calculate area of circle (PI * r * r). Macro should have 1 argument--the radius. Use preprocessor directive to define symbolic constant PI to use in the macro. Assign value to the radius in the main body of program and calculate area using the AREA macro, then output the area to screen. Output should resemble:

"The area of circle with radius 3.75 is: 44.18"

 ``123456789101112131415161718192021`` ``````#include using namespace std; #define AREA (PI, r) ((PI) * (r * r)) #define PI 3.14 int main () { float a, r; r = 5; a = AREA (PI, r); cout << "area of a circle with radius 5 = "<< a << endl; system ("pause"); return 0; }``````

ERROR MESSAGE: [ERROR] expression cannot be used as a fuction*
*Referring to ((PI) * (r*r))

*also I use the system ("pause"); because I am using DevC++ and sometimes will not output without this direction....

I do not quite understand what is meant by "macro should have one argument--the radius". Isn't by definition of macro defined as AREA distinguish it from "radius" completely?
C and C++ have a macro language attached. Macro languages just do text substitution. It is possible to just run the preprocessor to process this macro language, and see the output.

When this run over your program, this is the relevant part of what's produced. So the syntax error should not be obvious.
 ``123456789101112131415`` ``````int main () { float a, r; r = 5; a = (3.14, r) ((3.14) * (r * r)) (3.14, r); cout << "area of a circle with radius 5 = "<< a << endl; system ("pause"); return 0; }``````

The C and C++ language have functions that are more suitable for what you want to do.

You were asked to write a macro that takes one argument, the area really is just a function od the radius.
 ``123456789101112131415161718`` ``````#include using namespace std; #define PI 3.14 #define AREA(r) ((PI) * (r * r)) int main () { float a, r; r = 5; a = AREA(r); cout << "area of a circle with radius 5 = "<< a << endl; system ("pause"); return 0; }``````
Last edited on
The error is non-obvious:

You have a space between the macro name and the macro arguments. The preprocessor doesn't like that.

Get rid of the space and it will work fine:

 ``4567`` ``````#define AREA(PI, r) ((PI) * (r * r)) // ^ no space #define PI 3.1415926 // ^ Give PI some better precision than three sig figs. ``````

Remember,
`#define QUUX (anything) `
is different than
`#define QUUX(arg,...) (anything) `

Hope this helps.
macros are really hard to debug and cause a number of problems if not used very carefully. I agree with making a function instead.
PI is ok as a "macro"; (though there are better ways to do that too); I am speaking only of function-macros (those are 'bad' in most cases).

consider
double area(double r)
{return PI*r*r);}

which can be debugged with step intos, it can be listed as a problem cleanly if there is a syntax issue, it is type-safe, and it is all around safer and cleaner. And if you add a space before the parameters, it still works, its tolerant to standard c++ allowances where whitespace is mostly irrelevant (in a few very specific cases lack of whitespace blows up, when the compiler can't tell an operator from an angle bracket usage).

Sharp eyes on the fix though, nice find.
Last edited on
Macros have very small use in C++. But, y'all, his assignment reads “Write program that defines macro called AREA...”

When asked to write a macro, write a macro.
Topic archived. No new replies allowed.