0

It was challenging to word the title for this question, but I hope you can forgive me as I will elaborate here. The problem i'm having is my C program floods the console with "Enter the student's grade: F" if I enter anything other than an integer. I'm new to C, so I don't understand how to check if the input is of the valid type.

int main() {
  int grade; //number 0-10 associated with the letter grade
  while (1) {
    printf("Enter the student's grade: ");
    scanf("%i", &grade);
    switch (grade) {
      case 10: printf("A \n"); break;
      case 9: printf("A \n"); break;
      case 8: printf("B \n"); break;
      case 7: printf("C \n"); break;
      case 6: printf("D \n"); break;
      case 5: printf("F \n"); break;
      case 4: printf("F \n"); break;
      case 3: printf("F \n"); break;
      case 2: printf("F \n"); break;
      case 1: printf("F \n"); break;
      case 0: printf("F \n"); break;
      default: printf("Please enter a valid test score \n"); break;
    }
  }
  return 0;
}

Thanks for the help!

dda
  • 6,030
  • 2
  • 25
  • 34
Digglit
  • 576
  • 1
  • 4
  • 11

2 Answers2

2

Check if scanf succeeded.

int nread = scanf("%i", &grade);
if (nread != 1) // scanf failed, start cleanup 
{
    scanf("%*[^\n]%*c");
}

Look for any book and you'll know that scanf returns number of elements that were successfully read, so if you type a letter, it will fail to read an integer and return 0 (nothing read), then you can know there's something wrong and discard wrong stuffs.

Since scanf don't read anything if it encounters an error, the wrong stuff will remain in the input buffer, and badly it'll break the next scanf, leading to an infinite output flushing.


P.S. You don't need to repeat the statements after case 5 4 3..., just merge them into one:

 case 5: // Remove these and leave the last one there
 case 4:
 case 3:
 case 2:
 case 1:
 case 0: printf("F \n"); break;
iBug
  • 35,554
  • 7
  • 89
  • 134
0

Check the return of scanf. 1 means an integer was successfully scanned. 0 means the input was not an integer. Clean the input stream and try again.

#include <stdio.h>

int main ( void) {
    int grade; //number 0-10 associated with the letter grade
    int valid = 0;
    while (1) {
        do {
            printf("Enter the student's grade: ");
            if ( 1 != ( valid = scanf("%i", &grade))) {// 1 is success
                if ( EOF == valid) {
                    printf ( "found EOF\n");
                    return 0;
                }
                while ( '\n' != getchar ( )) {//clear input stream
                }
            }
        } while ( !valid);
        switch (grade) {
            case 10: printf("A \n"); break;
            case 9: printf("A \n"); break;
            case 8: printf("B \n"); break;
            case 7: printf("C \n"); break;
            case 6: printf("D \n"); break;
            case 5: printf("F \n"); break;
            case 4: printf("F \n"); break;
            case 3: printf("F \n"); break;
            case 2: printf("F \n"); break;
            case 1: printf("F \n"); break;
            case 0: printf("F \n"); break;
            default: printf("Please enter a valid test score \n"); break;
        }
    }
    return 0;
}
xing
  • 2,125
  • 2
  • 14
  • 10
  • Thank you very much for the answer (thanks to everyone else too). This definitely helps me understand how to check for what it is I was confused about! – Digglit Oct 04 '17 at 22:20