Possible fix:
#include <stdio.h>
int main (void) /* it is better to add void */
{
int counter = 0;
int sum = 0; /* no average is here */
int grade = 0;
int check;
for(;;) /* make it to an infinite loop, break later */
{
/* put this block inside the loop */
printf("Introduce grade:");
check = scanf("%d",&grade); /* don't assign scanf's return variable to grade, which deletes the grade read */
if (check != 1) /* check if a integer was read */
{
puts("Invalid input. Panik!");
return 1;
}
if (grade <= 0) /* !(grade > 0) */
{
break;
}
if (grade <0 || grade >100) /* && -> || */
{
printf("Invalid number");
}
else
{
sum += grade;
counter++; /* made to be done only if grade>=0 && grade<=100 */
}
}
printf("average = %lf", (double)sum/counter); /* add cast to double */
return 0; /* it may be better to add return 0; */
}
2
solved Can someone tell me whats wrong with my code and try to explain it? [closed]