2017-05-07 5 views
-1

私は今このコードを何時間も手伝ってきましたが、シンプルであっても、何が間違っているのか分かりません。それは論理ですか?または、問題は構文関連の問題ですか?カウンタとアキュムレータが動作せず、プログラムがクラッシュする。私は間違って何をしていますか?

私は、今月レースで個別に走ったキロメートルの数を入力するようにプログラムに指示します。 プログラムは、各レースでどのくらい走ったかを平均で伝えます。さらに騒ぎがなければ

は、ここでのコードは次のとおりです。ループ内

#include <stdio.h> 

main() 
{ 
    int STOP_VALUE = 8 ; /* you pick this number - outside the valid data set */ 

    int avg; 
    int currentItem; 
    float runningTotal = 0 ; 
    int counterOfItems = 0 ; 

    printf("Enter first item or 8 to stop: "); 

    scanf("%d", &currentItem); 

    while (currentItem != 8) { 

      runningTotal += currentItem; 

     ++counterOfItems; 

     printf("Enter next item or 8 to stop: "); 
     scanf("%d", currentItem);  

} 

    /* protect against division by 0 */ 
    if (counterOfItems != 0) 
    { 

      avg = runningTotal/counterOfItems ;} 
    else { 



    printf("On average, you've run %f per race and you've participated in %f running events. Bye! \n", runningTotal, counterOfItems); 
    } 

    return 0; 
} 
+0

期待される出力は何ですか?代わりに何を得ますか? – DyZ

答えて

1

scanf("%d", currentItem); 

main()は、少なくとも、int main(void)あるべき、と述べた

scanf("%d", &currentItem); 
      ^^ 

にする必要がありますホスト環境の標準に準拠しています。

1

あなたのavg変数はint型です。丸められると、奇妙な結果に終わる可能性があります。

実際には、あなたのavg変数は更新されていません。

関連する問題