Program that assigns grade

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
int score;
char grade;

printf("please enter a score\n");
while((n = scanf("%i",&score)) != EOF)
switch(score)
{
case 10:
case 9:
grade = 'A';
printf("your grade is %c\n",grade);
break;
case 8:
grade = 'B';
printf("your grade is %c\n",grade);
break;
case 7:
grade = 'C';
printf("your grade is %c\n",grade);
break;
case 6:
grade = 'D';
printf("your grade is %c\n",grade);
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'F';
printf("your grade is %c\n",grade);
break;
default: printf("error,invalid character data\n"); //invalid char data
}

system("pause");
return 0;
}


Hi, the output is right for everything except invalid char data.
when i type in a score greater than 10 or less then 0 it says "error,invalid character data" perfectly but when i type in a character example 'a' it outputs "error,invalid character data" like a million times and wont stop.
Please help!
That's because scanf() is trying to get an integer and it doesn't see one in the string you inputted. The while loop keeps running but you never get to do another scanf() since it is basically in error state. I'm not very familiar with C functions so I don't know of any way to fix it.
Topic archived. No new replies allowed.