C-计数器和累加器无法正常工作,并使程序崩溃.我究竟做错了什么



我已经使用此代码了几个小时了,尽管简单,但我找不到问题。是逻辑吗?还是问题是与语法相关的问题?

我希望该程序要求用户输入一个数字,以指示本月在比赛中单独运行多少公里。该计划平均每场比赛都告诉他们多少。

不进一步的ADO,这是代码:

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

在循环中

  scanf("%d", currentItem);

应该是

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

也就是说,main ()至少应为int main(void),以符合托管环境的标准。

您的AVG变量是一个int,它将被舍入,您可能会得到一些奇怪的结果。

实际上是一个更新,您的AVG变量未使用。

最新更新