Assistance with Functions

I need help with my functions and probably my ifs. I shouldn't have outputs in my functions but I don't know how to execute that. This program gives the choice of finding the area and perimeter of either a circle, triangle, and rectangle.

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
97
98
99
100
101
102
#include <iostream>
#include <cmath>

using namespace std;

double pi = 3.14;

void areaRectangle()
{
	double length;
	double width;

	cout << "What is your length and width: (Area) " << endl;
	cin >> length >> width;

	double area = length * width;

	cout << "The area of the rectangle is: " << area << endl;
}

void perimeterRectangle()
{
	double length;
	double width;

	cout << "What is your length and width: (Perimeter) " << endl;
	cin >> length >> width;

	double perimeter = 2 * length + 2 * width;

	cout << "The perimeter area of the rectangle is: " << perimeter << endl;
}

void areaTriangle()
{
	double base;
	double height;

	cout << "What is your base and height measurements: (Area) " << endl;
	cin >> base >> height;

	double area = base * height / 2;

	cout << "The area of the triangle is: " << area << endl;
}
    
    //@param: length1 is the first length of the triangle
	//@param: length2 is the second length of the triangle
	//@param: length3 is the third length of the triangle
void perimeterTriangle()
{
	double length1; 
	double length2; 
	double length3;
	
	cout << "What is the length of your three sides: (Perimeter) " << endl;
	cin >> length1 >> length2 >> length3;

	double perimeter = length1 + length2 + length3;
	
	cout << "The perimeter of your triangle is: " << perimeter << endl;


}
   
void areaCircle()
{
	double radius;

	cout << "What is your radius: (Area) " << endl;
	cin >> radius;

	double circumference = 2 * pi * radius;
	double area = pi * radius * radius;

	cout << "Your circumference is " << circumference << " and your area is " << area << endl;
}

int main()
{
	cout << "Which shape do you want to know the area and perimeter of? (circle = 1, rectangle = 2, triangle = 3)" << endl;
	int shapeChoice;
	cin >> shapeChoice;

	if (shapeChoice == 1)
	{
		areaCircle();
	}
	else if (shapeChoice == 2)
	{
		areaRectangle();
		perimeterRectangle();
	}
	else (shapeChoice == 3);
	{
		areaTriangle();
		perimeterTriangle();
	}
	

	return 0;
}
closed account (48T7M4Gy)
Try this:

1
2
3
4
[code]int areaRectangle(int length, int width)
{
    return length * width;
}

and ...

1
2
3
4
5
6
7
8
9
10
int main()
{
   cout << areaRectangle( 6, 5) << endl;

   int len= 2;
   int wid= 7;
   cout << areaRectangle( len, wid ) << endl;

   return 0;
}

Last edited on
closed account (48T7M4Gy)
and this ...

1
2
3
4
5
6
7
if (shapeChoice == 1)
	{
		cout < areaCircle( 6 );
	}
	else if (shapeChoice == 2)
	{
		cout << areaRectangle( 2, 78);etc ...

if's looked OK except you need to pass parameters to functions and let them return the results to be printed out in main()
Topic archived. No new replies allowed.