I've been toying around with this code for some hours now and even though simple I cannot find what is wrong with it. IS it the logic? OR the problem is a syntax-related one?
I want the program to ask the user to enter a number indicating how many kilometres they've run individuately in races this month. The program will tell them on average how much they've run in each race.
Without further ado, here's the code:
#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;
}
Inside the loop
scanf("%d", currentItem);
should be
scanf("%d", ¤tItem);
^^
That said, main ()
should be int main(void)
, at least, to conform to the standard for a hosted environment.