C ++函数输出 [英] C++ function output
问题描述
我试图在这个程序中输出一个三角形的计算区域,但是当我尝试从函数输出区域时,我得到了字母和数字的混合而不是答案,如果有人能指出我在做什么
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.
我已经厌倦了tArea(区域),但它给出了一个不同的错误。
I have tired tArea(area) but it gives a different error.
推荐答案
你没有调用函数。当你使用没有方括号的tArea名时,C ++会给你函数的地址而不是调用它,这将以十六进制格式打印。
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;
}
这篇关于C ++函数输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!