2017-05-07 1 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); 

이어야

말했다
scanf("%d", &currentItem); 
      ^^ 

, main() 적어도,에, int main(void)해야 호스팅 된 환경의 표준을 준수해야합니다.

1

변수가 int이고, 반올림 될 경우 일부 이상한 결과가 발생할 수 있습니다.

실제로 업데이트로, 귀하의 평균 변수는 사용되지 않습니다.

관련 문제