My code is supposed to take in a numeric score and return the grade it will receive. My code correctly returns an error message for all invalid input except alphanumeric. With alphanumeric input, it just takes the number and runs as usual. While this is an assignment, I have done the preliminary meeting with my professor and I am allowed to ask for help for this particular error.
I have tried using isdigit, isalphanum, other using other data types than float. Thank you
Here is the code:
#include <stdio.h>
int main()
{
float score; // Declare a variable to store the score
char grade; // Declare a variable to store the letter grade
printf("Insert Score: n"); // Prompt user for input
scanf("%f", &score); // Read the score
// Assign a letter grade to score according to the provided standards
if (score >= 80 && score <= 100) {
grade = 'A';
}
else if (score >= 70 && score < 80) {
grade = 'B';
}
else if (score >= 60 && score < 70) {
grade = 'C';
}
else if (score >= 50 && score < 60) {
grade = 'D';
}
else if (score >= 0 && score < 50) {
grade = 'F';
}
else {
printf("Invalid Input. Try Again. n"); //Filter out invalid input
}
printf("Your score is %0.2f and your grade is %c n", score, grade);
//Print out grade and accompanying grade
return 0;
}
Karis C. Anoruo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
15