2017-09-23 45 views
-2

我正在試圖創建一個程序,計算Vt給定的值Vt,a和tm。當我運行代碼時,程序首先詢問第一個問題,然後給它一個值,然後通過代碼加速,而不讓我給其他兩個變量賦值。我用C編程。這是代碼:我該如何修復我的代碼?我認爲這個問題是第一個scanf?

#include <stdio.h> 

main() { 

    float Vt; 
    float Vo; 
    float a; 
    float tm; 

    printf(" At what time in flight do you wish to know the velocity? To the 
    nearest hundredth. :"); 
    scanf(" %.2f", &tm); 

    printf (" What is the angle of trajectory? :"); 
    scanf (" %.2f", &a); 

    printf (" What is the initial velocity? :"); 
    scanf (" %.2f", &Vo); 

    float sina = sin (a); 
    float cosa = cos (a); 
    float tana = tan (a); 

    Vt = sqrt((pow((Vo * cosa), 2.0)) + (pow((Vo * sina - (9.8 *tm)), 2.0))); 

    printf("\n\n\n\n %.3f", Vt); 

    return 0; 

} 
+0

https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c的可能的複製 –

+2

你的主要是真的老了,你應該升級! –

+3

嘗試使用'%f'而不是'%.2f' – BLUEPIXY

回答

3

您的scanf格式字符串無效。將它們更改爲"%f"。也就是說,改變

  • scanf(" %.2f", &tm);scanf("%f", &tm);
  • scanf(" %.2f", &a);scanf("%f", &a);
  • scanf(" %.2f", &Vo);scanf("%f", &Vo);

如果您打算掃描最多N個字符,則必須使用"%Nf"格式字符串(如"%2f""%3f"),其中N是大於0的整數。