So I’m trying to return the perimeter and area in int main, but g++ keeps throwing error messages. Please help in explanation of error. Thanks.
Source:
1 // This program takes in two values and returns the perimeter and the area of the values taken.
2
3 #include<iostream>
4 using namespace std;
5
6 double perimeter (double a, double b);
7 double area (double a, double b);
8
9 int main ()
10 {
11 // initialization
12 double x, y, r, q;
13
14 // inputs
15 cout << "Please enter the first value (no units): " << endl;
16 cin >> x;
17 cout << "Please enter the second value (no units): " << endl;
18 cin >> y;
19
20 // perimeter
21 cout << "The perimeter is: " << r << endl;
22 r(x, y);
23
24 // area
25 cout << "The area is: " << q << endl;
26 q(x, y);
27
28 return 0;
29 }
30
31 double perimeter (double a, double b)
32 {
33 2.0 * (a + b);
34 return 0;
35 }
36
37 double area (double a, double b)
38 {
39 a * b;
40 return 0;
41 }
Output:
x@xLinux:~$ g++ -Wall rectangle.cpp
rectangle.cpp: In function ‘int main()’:
rectangle.cpp:22:11: error: ‘r’ cannot be used as a function
rectangle.cpp:26:11: error: ‘q’ cannot be used as a function
rectangle.cpp: In function ‘double perimeter(double, double)’:
rectangle.cpp:33:18: warning: statement has no effect
rectangle.cpp: In function ‘double area(double, double)’:
rectangle.cpp:39:10: warning: statement has no effect
Because you have defined
qandras doubles, then you are trying to ‘call’ them like a function with your()sEdit
Also, like the other answers state, depending on what you want to do – you either need to define
qandras functions (like you have done withareaandperimeteror you need to call your two existing functions onxandye.g.area(x, y)