Need Help with Function; program not working

Hello,
Could someone direct me to the build a better understanding of functions. I need to create a program that computes the area of different programs. My functions need to be validated

#include <iostream>
using namespace std;
/* this program will present the user with a menu and allow the user
to calculate different type of geometric shape */


const double PI=3.1416;
const double AREC (int length)(int width);
const double ACIRCLE= PI( radius*radius);
const double ATRIANGLE= (height*base)/2;


int main()
{
int option,length,width,radius,height,base;


do
{

cout << "\n1- Compute the area of a Rectangle : "
<< "\n2- Compute the area of a Circle: "
<< "\n3- Compute the area of a Triangle : "
<<"\n4- Exit Program " << endl;

cout << "\n\tPlease select an option : ";
cin >> option;

if(option == 1)
{
cout << "\nPlease enter length of the Rectangle : ";
cin >> length;

cout << "\nPlease enter the width of the Rectangle : ";
cin >> width;

cout << "The area of the Rectangle is : " << AREC() ;

}
else if(option == 2)
{
cout << "\nPlease enter the radius of the Circle: "
cin>> radius ;

cout <<"\n The area of the Cirlce is: "
<< ACIRCLE();
}
else if(option == 3)
{
cout<<" In order to calculate the Area of the triangle;"
<< " we must input the base of the triangle, followed by the height of the triangle"
<< "\nPlease enter the base of the triangle";
cin >> base;

cout << "Now, please enter the Height of the triangle: ";
cin >> height;

cout << " The area of the Triangle is: "<< ATRIANGLE();
}
else if(option == 4)
{
cout << "Terminating Program" << endl;
}
else
{

cout << "Invalid Option entered" << endl;
}
}
while(option != 4);
return 0;
}
Last edited on
One thing:
You have declared :
 
const double AREC (int length)(int width);


Do you mean a function ?
If yes, you have declared wrong function because parameters are declared in the same braces.
fantomasAlbania,

Yes, a function. Could a function work without parameters? if so should I have defined AREC inside main?
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

http://www.cplusplus.com/doc/tutorial/functions/

Look at the switch statement section in here: http://www.cplusplus.com/doc/tutorial/control/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream> 
#include <cmath> // added cmath
using namespace std;
/* this program will present the user with a menu and allow the user
to calculate different type of geometric shape */


const double PI = 3.1416;
double AREC (double length, double width); // fixed (), changed to doubles
double ACIRCLE (double radius); // added variable types, removed *, remover =, removed pi, removed one of the radius
double ATRIANGLE (double height, double base);// added variable types, removed *, removed /2


int main()
{
	int option;
	double length, width, radius, height, base;


	do
	{

		cout << "\n1- Compute the area of a Rectangle : "
			<< "\n2- Compute the area of a Circle: "
			<< "\n3- Compute the area of a Triangle : "
			<< "\n4- Exit Program " << endl;

		cout << "\n\tPlease select an option : ";
		cin >> option;

		if (option == 1)
		{
			cout << "\nPlease enter length of the Rectangle : ";
			cin >> length;

			cout << "\nPlease enter the width of the Rectangle : ";
			cin >> width;

			cout << "The area of the Rectangle is : " << AREC(length, width);

		}
		else if (option == 2)
		{
			cout << "\nPlease enter the radius of the Circle: ";// added ;
				cin >> radius;

			cout << "\n The area of the Cirlce is: "
				<< ACIRCLE(radius);
		}
		else if (option == 3)
		{
			cout << " In order to calculate the Area of the triangle;"
				<< " we must input the base of the triangle, followed by the height of the triangle"
				<< "\nPlease enter the base of the triangle";
			cin >> base;

			cout << "Now, please enter the Height of the triangle: ";
			cin >> height;

			cout << " The area of the Triangle is: " << ATRIANGLE(height, base);
		}
		else if (option == 4)
		{
			cout << "Terminating Program" << endl;
		}
		else
		{

			cout << "Invalid Option entered" << endl;
		}
	} while (option != 4);
	return 0;
}

//added functions

double AREC(double length, double width)
{
	double area;
	area = length * width;
	return area;
}

double ACIRCLE(double radius)
{
	double area;
	area = PI * pow(radius, 2);
	return area;
}

double ATRIANGLE(double height, double base)
{
	double area;
	area = (height * base) / 2;
	return area;
}


Would also recommend using switch statment instead of multiple if statments, just looks nicer.
joe864864 ,
thank you. I thought everything needed to be defined before the int main. Could I have used void?

integralfx,
Yes, I did post twice, did not know which one would be the best place to post and ask away. Because I am very much a beginner and its also C++ programming. sorrry
You could use void but then you would have to create the variable area in main and pass it to the functions via reference. I would leave it the way it is. If you have to return more than one value you need to pass them via reference, but if you are only returning one value like this it's easier and simpler to just do it the way I did.
Topic archived. No new replies allowed.