【问题标题】:C++ help! Program won't work [closed]C++ 帮助!程序不起作用[关闭]
【发布时间】:2013-12-27 05:27:33
【问题描述】:

我正在编写一个计算三角形斜边的程序,但不知何故,我遇到了一些意想不到的错误,或者更像是运行时错误,可悲的是,我什至不知道自己的错误.无论如何,这是我的代码,我会很高兴得到答案

#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));
}

【问题讨论】:

  • 您到底遇到了什么错误?
  • 那么,你的问题是它计算了错误的值,还是别的什么?
  • 你确实忘记在小边函数中取平方根。
  • 不是这样,每次我运行这个程序都会给我错误,不是这样,它甚至不会突出我的错误
  • 请编辑您的帖子并包含有关错误的详细信息。它是否返回错误的值?它会崩溃吗?它是用咖啡代替茶吗?

标签: c++ visual-studio-2010 visual-c++ runtime-error


【解决方案1】:

假设您的意图是使用旧的 pythag,您的函数需要在其中调用 sqrt:

double hypothenuse(double leg1, double leg2){
    return sqrt((leg1 * leg1) + (leg2 * leg2));
}

这当然需要您包含&lt;cmath&gt; 标头

【讨论】:

    【解决方案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));
    }
    

    【讨论】:

    • 哇哦!谢啦。我很感激:)
    • 我想知道为什么您的编译器没有警告它;)如果它解决了您的问题,请考虑对其进行投票并“接受答案”。不客气 :)
    • 如果我有足够的声誉,我会投票的。哈哈
    • 好吧,如果这解决了您的问题,您仍然可以投票并接受答案...您不需要声誉来投票或接受;)
    【解决方案3】:

    根据你的代码visual studio会报这个错误

    LNK2019: unresolved external symbol "double __cdecl hypotenuse(double,double)"  
    (?hypotenuse@@YANNN@Z) referenced in function _main   
    

    因为在函数定义中您拼写为hypothenuse,但在函数声明中您将其拼写为hypotenuse(拼写中缺少h)。
    这导致链接时间错误,因为它找不到斜边的函数定义。
    更正它,它将编译并进行RichardPlunkett建议的逻辑更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 2020-04-25
      • 2012-04-11
      • 2012-04-05
      相关资源
      最近更新 更多