for loops & switch additional input

how can i fix this code, im trying to learn C,
well basically it works, but it enters an unecessary input after, a valid one, so the loop would end after 5 times, while it should be 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

 int main(void){
     char grade;
     int counter = 0;
     

     for (counter = 0; counter < 10; counter++){
              printf("please enter your grade: \n");
              scanf("%c", &grade);
              
                 switch(grade){
                   case 'A' : printf("WOW AMAZING!!!!\n");
                   break;
                   case 'B' : printf("B-MAZING!!!!\n");
                   break;
                   case 'C' : printf("C-MAZING!!!!\n");
                   break;
                   case 'D' : printf("D-AZING!!!!\n");
                   break;
                   case 'E' : printf("E-MAZING!!!!\n");
                   break;
                   case 'F' : printf("FUCK!!!!\n");
                   break;
                   default : printf("please enter a valid grade\n");
                   break;
                           }
       }
     
     system("pause");
     return 0;
}
It works as written: http://coliru.stacked-crooked.com/a/f98c8480c2d11f70

However if you press enter after each input, there is some info for you: pressing enter adds another character to input stream (newline character) which is going to be read by %c.

To skip all whitespace characters, simply enter space before format specifier: scanf(" %c", &grade);
Last edited on
thank you! this helps!!
Topic archived. No new replies allowed.