1

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;
}
  • 5
    `i` is never incremented. Is that intentional? Don't you want to check every character in the `name`? – kaylum Jul 13 '21 at 06:54
  • 4
    `fflush(stdin);` does not reliably do what you think it does. – Yunnosch Jul 13 '21 at 07:00
  • `i` is never reset for the next string test. The place to input another string (or report success) should not be within the character testing loop, it needs a flag and an enclosing loop. And don't forget to [Remove trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Jul 13 '21 at 07:03
  • Welcome to StackOverflow! Please take the [tour] and read "[ask]". -- Did you debug your program? If not, please learn to debug. It is an important art for any developer, and pays immediately. – the busybee Jul 13 '21 at 07:14
  • 1
    Welcome to StackOverflow and programming! You should learn how to use a debugger to execute your code line by line, checking the variables at each step to be sure their value is what you expect. – fpiette Jul 13 '21 at 07:17
  • 1
    `fflush(stdin);` is *Undefined Behavior* (on all but a very few implementations, like yours) See `fflush` is not defined for *input* streams, see: [C11 Standard - 7.21.5.2 The fflush function(p2)](http://port70.net/~nsz/c/c11/n1570.html#7.21.5.2p2) and [fflush(stdin); **leads to undefined behaviour on most systems.**](https://stackoverflow.com/a/38325926/2173917). – David C. Rankin Jul 13 '21 at 07:35

1 Answers1

1

As pointed out by people in comments, you seem to be doing some mistakes in your code. While taking string input using fgets you're not removing trailing newline character. You're never incrementing i nor resetting it whenever you take next input on failed validation. But I think for validating string you want to validate whole string not just first character. I would suggest making a dedicated method for validation like this:

#include <stdbool.h>
#include <string.h>

bool validate(char name[]) {
    int len = strlen(name);
    for (int i = 0; i < len; i++) {
        if (isalpha(name[i]) == 0)
            return false;
    }
    return true;
}

Then you can simply call this method from you main method. Also instead of using value of name[i] in while loop terminating condition. I would suggest using the return value of validate to check whether to continue taking new inputs or not.

I cleaned up your code slightly (I used do-while loop instead) and this is how it looked afterwards:

char name[100];
bool isValid;
do {
   // Take String as input
   printf("\nFull Name: ");
   fgets(name, 100, stdin);
   // Remove trailing newline added by fgets
   name[strcspn(name, "\n")] = 0;
   
   // Validate
   isValid = validate(name);
   if (!isValid) {
       printf("\nPlease enter your Full Name correctly!");
   } else {
       printf("Good\n");
   }
   // Continue ?
} while (!isValid);
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40