C 计数器和累加器不工作,导致程序崩溃。我做错了什么?

C 计数器和累加器不工作,导致程序崩溃。我做错了什么?,c,if-statement,while-loop,counter,accumulator,C,If Statement,While Loop,Counter,Accumulator,我已经玩弄这段代码好几个小时了,尽管很简单,我还是找不到它有什么问题。这是逻辑吗?或者问题与语法有关 我希望程序要求用户输入一个数字,表明他们在本月的比赛中各自跑了多少公里。 该计划将告诉他们在每一场比赛中平均跑了多少 不用多说,下面是代码: #include <stdio.h> main () { int STOP_VALUE = 8 ; /* you pick this number - outside the valid data set */ int av

我已经玩弄这段代码好几个小时了,尽管很简单,我还是找不到它有什么问题。这是逻辑吗?或者问题与语法有关

我希望程序要求用户输入一个数字,表明他们在本月的比赛中各自跑了多少公里。 该计划将告诉他们在每一场比赛中平均跑了多少

不用多说,下面是代码:

#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;  
} 
#包括
主要()
{
int STOP_VALUE=8;/*您选择此数字-在有效数据集之外*/
国际平均值;
int当前项目;
浮动运行总计=0;
int counterOfItems=0;
printf(“输入第一项或8停止:”;
scanf(“%d”和当前项);
while(当前项!=8){
runningTotal+=当前项目;
++反项目;
printf(“输入下一项或8停止:”;
scanf(“%d”,当前项);
}
/*防止被0除法*/
如果(计数器项!=0)
{
平均值=运行总计/计数器项;}
否则{
printf(“平均而言,您每场比赛跑了%f次,您参加了%f次跑步项目。再见!\n”,runningTotal,counterOfItems);
}
返回0;
} 
在循环内部

  scanf("%d", currentItem);
应该是

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

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

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


实际上是一个更新,没有使用avg变量。

预期输出是什么?你得到了什么?