2014-03-02 56 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