2014-03-02 57 views
0

我是C的新手。我想编写一个程序来计算Hypotenuse,但是当我运行它时,我得到了垃圾值,我不知道如何处理它。我怎么处理垃圾?

double Hypotenuse(double base, double perpendicular) 
{ 
    double hyp = sqrt((base*base) + (perpendicular*perpendicular)); 

    return hyp; 
} 

int _tmain(void) 
{ 
    double b, p; 

    printf("Enter the lenght of base\n"); 
    scanf_s("%f",&b); 

    printf("Enter the lenght of perpendicular\n"); 
    scanf_s("%f",&p); 

    printf("The hypotenuse of the triangle is %3.3f",Hypotenuse(b,p)); 

    return 0; 
} 
+1

使用' “%LF”''为 – BLUEPIXY

+0

double' @BLUEPIXY为什么会这样?你的意思是在scanf?我在看printf。 –

+1

喜欢你的标题 – bolov

回答

1

问题出在您的scanf语句中。 作为一个拇指规则,你应该总是使用“%lf”作为双打。 这完美的作品:

double Hypotenuse(double base, double perpendicular) 
{ 
    double hyp = sqrt((base*base) + (perpendicular*perpendicular)); 

    return hyp; 
} 

int _tmain(void) 
{ 
    double b, p; 

    printf("Enter the lenght of base\n"); 
    scanf_s("%lf",&b); 

    printf("Enter the lenght of perpendicular\n"); 
    scanf_s("%lf",&p); 

    printf("The hypotenuse of the triangle is %3.3lf",Hypotenuse(b,p)); 

    return 0; 
} 
0

所以,看看有什么是你的代码怎么回事或者u得到垃圾,毕竟SCANF打印值 前的最佳方式:

double a; 
scanf("%f",&a); 
printf("a=%f",a); //OR 
printf("a=%3.3f",a); 

,你可以很容易看到,如果问题出在您的scanf或其他地方..

+0

为什么没有真正的调试器? – deviantfan

+0

@deviantfan这个问题不是真正的调试器,我帮他理解调试器是如何工作的......或者我不知道..当我停在某个地方或者我有问题时我总是通过我的自我调试..那对我很好 – Rixxa