我正在编写一个程序来计算三角形的斜边但不知何故,我遇到了一些意想不到的错误或更像是运行时错误而且很遗憾的是,我甚至不知道自己的错误。无论如何,这是我的代码,我将很高兴答案
#include <iostream>
using namespace std;
//function prototype
double hypotenuse(double leg1, double leg2);
double leg(double hypotenuse, double leg);
void main()
{
//local variable
double leg1;
double leg2;
//user interface
cout << "Enter the first leg of the triangle: ";
cin >> leg1;
cout << "Enter the second leg of the triangle: ";
cin >> leg2;
cout << "The value of hypothesis is: " << hypotenuse(leg1,leg2) << endl;
system("pause");
}
double hypothenuse(double leg1, double leg2)
{
return ((leg1 * leg1) + (leg2 * leg2));
}
答案 0 :(得分:2)
假设你的意图是使用旧的pythag,你的函数需要一个sqrt调用:
double hypothenuse(double leg1, double leg2){
return sqrt((leg1 * leg1) + (leg2 * leg2));
}
这当然要求您包含<cmath>
标题
答案 1 :(得分:2)
请拼写检查!
double hypo**te**nuse(double leg1, double leg2);
cout << "The value of hypothesis is: " << hypo**te**nuse(leg1,leg2) << endl;
double hypo**the**nuse(double leg1, double leg2)
{
return ((leg1 * leg1) + (leg2 * leg2));
}
答案 2 :(得分:2)
根据你的代码,visual studio会给出这个错误
LNK2019: unresolved external symbol "double __cdecl hypotenuse(double,double)"
(?hypotenuse@@YANNN@Z) referenced in function _main
因为在函数定义中你拼写了hypothenuse
但是在函数声明中你拼写为hypotenuse
(拼写中缺少h
)。
这导致链接时间错误,因为它找不到斜边的功能定义
更正它,它将编译并进行RichardPlunkett
建议的逻辑更改。