I am trying to make a program to allow the user to input their full name with only characters that can be accepted. However, when I input a digit after a character the validation is skipped. I've tried with a for loop - for (int i = 0, k = strlen(name); i < k; i++), it still doesn't work, and when I change to a while loop it is still accepting digits after a character. I've tried putting the ELSE block outside of the loop as well.
Here is my code with a while loop:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char name[100];
int i = 0;
printf("Full Name: ");
fflush(stdin); // used to clear the buffer and accept the next string
fgets(name, 100, stdin);
while (name[i])
{
if (!isalpha(name[i]))
{
printf("\nPlease enter your Full Name correctly!");
printf("\nFull Name: ");
fgets(name, 100, stdin);
}
else
{
printf("Good\n");
break;
}
}
printf("NEXT\n");
return 0;
}