Why i get the address instead of the result?

Im doing a question that asked to include a header of area function to find the area of the triangle if the input is valid.
Here are my codes for the header and also the main function.
Why i get the address or some weird code for the area?

Header Code
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;



bool isValid(double side1,double side2, double side3)
{
	if ((side1>=0 || side2>=0 || side3>=0) && (side1+side2>side3) && (side1+side3>side2) && (side2+side3>side1))
	{return true;}

	return false;
	
}


double area(double side1, double side2, double side3)
{
	double area;
	double s;
	if(isValid(side1,side2,side3))
	  {s = (side1 + side2 + side3) / 2;
	  area = pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
	
	  return area;}
	
}



Main Function codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "MyTriangle.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


int main ()
{
	double side1, side2, side3;
	cout << "Enter three sides of a triangle: ";
	cin >> side1 >> side2 >> side3;
	
	if(isValid(side1, side2, side3))
	{cout << "Your input is valid. The area of the triangle is" << area << endl;}
	else
	{cout << "Your input is invalid." << endl;}

	return 0;

}


and this is my output image
http://imageshack.us/a/img40/5875/outputimg.jpg
Last edited on
Here is how to call the function named area:

area(value1, value2, value3)

Here is how you are trying to call it:
area

I see that you managed to call the function isValid correctly, so this should not be a surprise to you.
check the line in the main
{cout << "Your input is valid. The area of the triangle is" << area << endl;}

You didnt call the area function and you are just using undeclared and uninitialized area variable. Thats the error for your code

replace area with area(side,side2,side3)
I can conclude that you are using MS VC++ which incorrectly ouputs a function address when the name of a function is used in operator << instead of to output bool value true.:)

area - is not a call of a function. Function call has special syntax when after the function name alsit of argunebts enclosed in parentheses follows.
Last edited on
Hello Veeyik;

I to am a beginner at c++ but I found ths problem an interesting practice for me. It is my understanding that the area of a triangle is (base x height) / 2. So, I wrote a function that perfoms this. Here is the code.

1
2
3
4
5
6
7
8
9
10
float AreaofTri (float b1, float h1)
{
	float Area;
	Area = ((b1 * h1) / 2);
	if (b1 == 0 || h1 == 0)
		cout << "Invalid Entry. Try Again..";
	else 
		cout << "The area of this triangle is: " << Area;
	return Area;
}


you can put this into a header and all that is needed is the proper inputs and the function call to be placed in main and this should work.

I hope this helps
Last edited on
If you were asked to include the header file #include <cmath>
I don't think there is any need to write a function to calculate the area of a triangle cause the included header is supposed to contain a function to do that. but if it doesn't, then the included header file is useless in the code you wrote.
Okay, okay oooKay;

I needed some refreshing before looking further into this.
for calculating a three sided triangle and using the <cmath> header i rewrote the function as follows;

1
2
3
4
5
6
7
8
9
10
11
float AreaOfTri (float s1, float s2, float s3)
{
	float side = ((s1 + s2 + s3) / 2);	
	float CompArea = (side * ((side - s1) * (side - s2) * (side - s3)));
	float AreaTri = (sqrt (CompArea));
	if (s1 == 0 || s2 == 0 || s3 == 0)
		cout << "Invalid Entry...Please try again...";
	else	
	cout << "Area of three sided triangle is: " << AreaTri << " Squared";
	return AreaTri;
}


So far this works for me. Keep in mind I am a beginning programmer so if I need correcting feel free to contact me.

Here is my entire code for the problem.

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
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

float AreaOfTri ( float side1, float side2, float side3);					//function prototype

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Practice program for determining the area of a "
		<< "\ntriangle using all sides of the object" << endl << endl;
	float side1;
	float side2;
	float side3;
	cout << "Enter side 1: ";
	cin >> side1;
	cout << "Enter side 2: ";
	cin >> side2;
	cout << "Enter side 3: ";
	cin >> side3;
	AreaOfTri (side1, side2, side3);
	cout << endl << endl;
	system ("pause");
	return 0;
}

float AreaOfTri (float s1, float s2, float s3)
{
	float side = ((s1 + s2 + s3) / 2);	
	float CompArea = (side * ((side - s1) * (side - s2) * (side - s3)));
	float AreaTri = (sqrt (CompArea));
	if (s1 == 0 || s2 == 0 || s3 == 0)
		cout << "Invalid Entry...Please try again...";
	else	
	cout << "Area of three sided triangle is: " << AreaTri << " Squared";
	return AreaTri;
}

Hello guru's and gods of C++;

I did make an error here in the function.
under the conditional test
if (s1 == 0 || s2 == 0 || s3 == 0)

I changed it to check for negative numbers.
so now it looks like this.

if (s1 <= 0 || s2 <= 0 || s3 <= 0)

Now the function should output the error message if a negative number is inputted.

Topic archived. No new replies allowed.