getting void errors & trying to figure out if I'm doing functions right

I'm really bad at programming and my tutor didn't show up. I'm not sure if I'm doing the functions and if/else statements right. so if anyone could help me that would be amazing. Thanks.


The area of an arbitrary triangle can be computed using the formula:
area = sqrt s(s-a)(s-b)(s-c)
where a, b, and c are the lengths of the sides, and s is the semiperimeter.
s= a+b+c/2

Write a void function that uses five parameters: three value parameters that provide the lengths of the edges, and two reference parameters that compute the area and perimeter (not the semiperimeter). Make your function robust. Note that not all combinations of a, b, and c produce a triangle. Your function should produce correct results for legal data and reasonable results for illegal combinations.


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
  #include <iostream>
#include <cmath>
	using namespace std;
	void inputSides();
	void area(int semiperimeter, int sideA, int sideB, int sideC);
	void perimeter(int sideA, int sideB, int sideC);
	void semiperimeter(int sideA, int sideB, int sideC);
	void conditions(int sideA, int sideB, int sideC, int area, int perimeter);
	void output(int area, int perimeter);

int main()
	{
		char input = 'y';
		while (input == 'y') {
			inputSides();
			cout << "Enter y to repeat, any other key to quit " << endl;
			cin >> input;
		}

	}

	void inputSides() {
		int sideA;
		int sideB;
		int sideC;
		int semiperimeter;
		cout << "Enter a positive number for side A: " << endl;
		cin >> sideA;
		cout << "Enter a positive number for side B: " << endl;
		cin >> sideB;
		cout << "Enter a positive number for side C: " << endl;
		cin >> sideC;
		perimeter(sideA, sideB, sideC);
		area(semiperimeter, sideA, sideB, sideC);
	}

	void area(int semiperimeter, int sideA, int sideB, int sideC) {
		void area = sqrt((semiperimeter - sideA) * (semiperimeter - sideB) * (semiperimeter - sideC)); //incomplete type is not allowed, and 'area': illegal use of type 'void'
	}
	void semiperimeter(int sideA, int sideB, int sideC) {
		int semiperimeter = (sideA + sideB + sideC) / 2;
	}

	void perimeter(int sideA, int sideB, int sideC) {
		void perimeter = sideA + sideB + sideC; //incomplete type is not allowed, and 'perimeter': illegal use of type 'void;
	}
	void conditions(int sideA, int sideB, int sideC, int area, int perimeter) {
		if ((sideA + sideB > sideC) && (sideB + sideC > sideA)) {
			output(area, perimeter);
			if ((sideA + sideB > sideC) && (sideA + sideC > sideB))
				output(area, perimeter);
			if ((sideB + sideC > sideA) && (sideA + sideC > sideB))
				output(area, perimeter);
			if ((sideB + sideC > sideA) && (sideA + sideC > sideB));
			else {
				cout << "Not a triangle." << endl;
			}
		}
	}
	void output(int area, int perimeter) {
		cout << "Area is: " << area << endl;
		cout << "Perimeter is: " << perimeter << endl;
	}

Last edited on
line 38: void is not a valid data type for area. You probably want a double. If you want the area() function to return a value, then you need a return statement.

Line 41: You calculate semiperimeter, but don't do anything with the result. You need a return statement here also.

Line 45: void is not a valid type for perimeter. Return type of perimeter should be at least an int and you need a return statement here also.

37
38
39
40
41
42
43
44
45
46
47
48
49
50
	double area(int semiperimeter, int sideA, int sideB, int sideC) 
	{   double area = sqrt((semiperimeter - sideA) * (semiperimeter - sideB) * (semiperimeter - sideC)); 
	    return area;
	}
	
	int semiperimeter(int sideA, int sideB, int sideC) 
	{   int semiperimeter = (sideA + sideB + sideC) / 2;  // caution:  integer division here
	    return semiperimeter;
	}

	int perimeter(int sideA, int sideB, int sideC) 
	{   int perimeter = sideA + sideB + sideC; 	
             return perimeter;
	}


Last edited on
ok thanks now I'm getting this error

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
#include <iostream>
#include <cmath>
using namespace std;
void inputSides();
double area(int semiperimeter, int sideA, int sideB, int sideC);
int perimeter(int sideA, int sideB, int sideC);
int semiperimeter(int sideA, int sideB, int sideC);
void conditions(int sideA, int sideB, int sideC, int area, int perimeter);
void output(int area, int perimeter);

int main()
{
	char input = 'y';
	while (input == 'y') {
		inputSides();
		cout << "Enter y to repeat, any other key to quit " << endl;
		cin >> input;
	}

}

void inputSides() {
	int sideA;
	int sideB;
	int sideC;
	int semiperimeter;
	cout << "Enter a positive number for side A: " << endl;
	cin >> sideA;
	cout << "Enter a positive number for side B: " << endl;
	cin >> sideB;
	cout << "Enter a positive number for side C: " << endl;
	cin >> sideC;
	perimeter(sideA, sideB, sideC);
	area(semiperimeter, sideA, sideB, sideC); //uninitialized local variable 'semiperimeter' used
}

double area(int semiperimeter, int sideA, int sideB, int sideC)
{
	double area = sqrt((semiperimeter - sideA) * (semiperimeter - sideB) * (semiperimeter - sideC));
	return area;
}

int semiperimeter(int sideA, int sideB, int sideC)
{
	int semiperimeter = (sideA + sideB + sideC) / 2;
	return semiperimeter;
}

int perimeter(int sideA, int sideB, int sideC)
{
	int perimeter = sideA + sideB + sideC;
	return perimeter;
}
void conditions(int sideA, int sideB, int sideC, int area, int perimeter) {
	if ((sideA + sideB > sideC) && (sideB + sideC > sideA)) {
		output(area, perimeter);
		if ((sideA + sideB > sideC) && (sideA + sideC > sideB))
			output(area, perimeter);
		if ((sideB + sideC > sideA) && (sideA + sideC > sideB))
			output(area, perimeter);
		if ((sideB + sideC > sideA) && (sideA + sideC > sideB));
		else {
			cout << "Not a triangle." << endl;
		}
	}
}
void output(int area, int perimeter) {
	cout << "Area is: " << area << endl;
	cout << "Perimeter is: " << perimeter << endl;
}
Last edited on
now I'm getting this error

... what error is that, exactly?
area(semiperimeter, sideA, sideB, sideC);

You're passing the value "semiperimeter" to this function, and it's using it. What value does semiperimeter have? Where did you set that value?
Topic archived. No new replies allowed.