getting the surface of a cube with define

I'm trying to define the formula for the surface of a cube and than to call it in the code but I have a syntax error on the calling line. I think maybe i should use something different then SQRE but i don't know what .

#include<iostream>
#include <iomanip>
#include <vector>
#include<string>
#define SQRE (a,b,c) 2*(a*b+a*c+b*c)
using namespace std;


int main()
{
int a, b, c,P;
cout << "Please enter the lenght's of the site's: ";
cout << "Lenght of site a: ";
cin >> a;
cout << "Lenght of site b: ";
cin >> b;
cout << "Lenght of site c: ";
cin >> c;
P = SQRE(a, b, c);
cout<< P<< endl;
return 0;
}
Hello spax1111111,

You are using or misunderstanding the "#define". The compiler sees P = SQRE(a,b,c) as a call to a function, but the preprossor sees "SQRE" and replaces it with (a,b,c) 2*(a*b+a*c+b*c) and the entire lin would read P = (a,b,c) 2*(a*b+a*c+b*c)(a,b,c);. You will not see this replacement in your code, but it will be done when the code is compiled thus giving you an error.

You might try changing the "#define" to just 2*(a*b+a*c+b*c) and see what happens or write a function that returns an int.

Hope that helps,

Andy
Andy thank you very much this sounds very logical to me but i can make a function but i would like to do it like I "define" and if a change it a I still need to call it somehow i need a name of the "define"
Last edited on
#define SQRE (a,b,c) 2*(a*b+a*c+b*c)

You've got a space between 'SQRE' and '(a,b,c)'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#define rect_surface_area(a, b, c) (2 * ((a * b) + (a * c) + (b * c)))

int main() {

	int side_a = 5;
	int side_b = 4;
	int side_c = 2;

	int surface_area = rect_surface_area(side_a, side_b, side_c);

	std::cout << surface_area << std::endl;

	return 0;
}
that's it thank you very much this cplusplus site rules :D :D :D
Topic archived. No new replies allowed.