저는 몇 시간 동안이 코드를 가지고 놀았습니다. 단순한 것으로도 문제가 무엇인지 찾아 낼 수는 없습니다. 그것은 논리인가? 또는 문제는 구문과 관련된 것입니까?카운터 및 축전지가 작동하지 않고 프로그램이 중단됩니다. 내가 도대체 뭘 잘못하고있는 겁니까?
나는이 프로그램이 이번 달 경주에서 개별적으로 몇 킬로미터를 뛰었는지 나타내는 숫자를 입력하라고 사용자에게 묻고 싶습니다. 프로그램은 각 경주에서 얼마나 많이 뛰었는지 평균적으로 알려줍니다. 속히
, 여기에 코드입니다 :
#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", ¤tItem);
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;
}
예상되는 출력은 무엇입니까? 대신 당신은 무엇을 얻습니까? – DyZ