I am trying to output the calculated area of a triangle in this program but when I try to output the area from the function I get a mixture of letters and numbers instead of the answer, if someone could point out what I am doing wrong it would be most appreciated.
I have tired tArea(area) but it gives a different error.
You aren't calling the function. When you use the tArea name without the brackets, C++ will give you the address of the function instead of calling it, and this will be printed in hexadecimal format.
Also, you can use local variables instead of using parameters for working calculations:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
float tArea(float a, float b, float c);
int main()
{
float a, b, c;
cout << "Enter side 1: ";
cin >> a;
cout << "Enter side 2: ";
cin >> b;
cout << "Enter side 3: ";
cin >> c;
cout << "the area is" << tArea(a, b, c);
return 0;
}
float tArea(float a, float b, float c)
{
float s = (a + b + c) / 2;
float area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
When you do
cout << "the area is" << tArea;
You are sending data to the standard output . You can think of this as printing data to the console since that is where standard output normally goes.
In this case tArea
is just a reference that tells the computer where to find the actual tArea()
function code.
So you are actually just printing the reference tArea
which is an address. That address is a number, but the iostream cout
doesn't really know what that means. It tries to interpret that the best it can, and it comes out all garbled as letters and numbers.
What you want to do is call your area function and print the returned result. To do that you have to add parentheses to the end of the function call.
If you had an area function that took no parameters then you could call it like this tArea();
However in your code the function signature is as follows
float tArea(float a, float b, float c, float s, float area)
This means that in order for your function to return the area as a float, you have to pass in 5 parameters. All 5 of those parameters must be floats.
A valid call for you would be something like
tArea(2.0, 2.1, 1.8, 3.0, 4.0);
By changing this line
cout << "the area is" << tArea;
to this
cout << "the area is" << tArea(a, b, c, 1.0, 1.0);
You can solve your issue.
I do encourage you to go a step further though and change the following lines that are not related to your exact question. Change
float tArea(float a, float b, float c, float s, float area)
{
s = (a + b + c) / 2;
area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
to
float tArea(float a, float b, float c)
{
float s = (a + b + c) / 2;
float area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
then call it like this
cout << "the area is" << tArea(a, b, c);
This is better because it removes s and area from the function signature. Since they are not needed prior to the calculation that happens inside the area function, and they are immediately rewritten inside the function, we do not care what their values were before. That is why I showed you that they can both be passed in as 1.0. In reality they could be any float, and it would not matter since they are immediately overwritten.
s and area should be local variables specific only to the scope of the area function as IanM_Matrix1 has suggested. Doing it that way will allow those temporary variables to be destroyed earlier freeing up memory. Doing it this way is a better practice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.