I'm trying to create an array that allows me to enter the characters depending on the number that the user has previously entered, if I enter 10 I only want the user to enter 10 characters, A or F. The problem is that it does not work as expected, when entering the number it sends me to the while loop and does not let me exit.
#include <stdio.h>
int main() {
int i, students;
char grade[100];
printf("Welcome, enter the number of students to assign school grade: \n");
scanf("%d", &students);
printf("Enter A (Approved) or F (Failure)\n");
for (i = 0; i < students; i++) {
printf("School grade for student %d: \n", i + 1);
scanf("%c", &grade[i]);
while (grade[i] != 'A' || grade[i] != 'F') {
printf("Please enter a valid school grade: ");
scanf("%c", &grade[i]);
}
}
return 0;
}
After I enter the number 10, the program skips the second scanf and sends me into the while loop.
By changing scanf("%c", &grade[i])
into scanf (" %c", &grade[i])
, the problem is that now the while loop is held even when I enter A or F.