-1
set1:
printf("Name            : ");
gets (name);
if (isalpha(name)) {printf("\nSorry, input is invalid\n");
goto set1;}

this is a piece of my code, and i declared name as char name [30]; but, it says error argument of type *char is incompatible with parameter type int.. and how to validate if we input randomly alphabet and number together (e.g gghjhj88888)?

thank you for helping?

  • 1
    Post a complete test case. How is `stdNumb` declared? How is it initialized? ... – LihO May 09 '13 at 21:01
  • You should look into while loops – Sam I am says Reinstate Monica May 09 '13 at 21:02
  • 1
    I think you need some good book that covers basics. Then you need to start reading the documentation of functions you use: [isalpha](http://linux.die.net/man/3/isalpha) – LihO May 09 '13 at 21:04
  • i declared as char name [30]; @BLUEPIXY – Monica Panjaitan May 09 '13 at 21:08
  • `isalpha(name)` isalpha arg is one charactor not pointer to char --> `isalpha(*name)` : meant top char check. and do `#include ` – BLUEPIXY May 09 '13 at 21:09
  • 1
    this question seems to have an answer. you should look into it http://stackoverflow.com/questions/9753346/determine-if-a-c-string-is-a-valid-int-in-c – Sam I am says Reinstate Monica May 09 '13 at 21:10
  • @BLUEPIXY okay thanks for the tips, im done with validating if user input number, it's invalid.. but when user inputs mixing number and alphabet, it still doesn't work.. any suggestion? – Monica Panjaitan May 09 '13 at 21:18
  • I think that it can use isValidName of @VoidPointer is instead of isalpha If you need all of the characters are composed of the alphabet. – BLUEPIXY May 09 '13 at 21:22
  • if you want to check the first character isn't alphabetic then do `if (!isalpha(*name))` – BLUEPIXY May 09 '13 at 21:34
  • @BLUEPIXY finally, i can complete it :) thank you :) but, i got another validation, how to check if input cannot be more than 6 characters? dateOfBirth[7]; set4: printf("Date of Birth (DDMMYYY) : "); gets (dateOfBirth); if (isalpha(*dateOfBirth)) {printf("\nSorry, input is invalid\n"); goto set4;} – Monica Panjaitan May 09 '13 at 21:39
  • @MonicaPanjaitan I wrote the answer column because longer. – BLUEPIXY May 09 '13 at 22:28
  • Note that using `gets()` is always a very bad idea. Use `fgets()` (and deal with the newline) or [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) from POSIX 2008 (and remember to free the space when appropriate). – Jonathan Leffler May 12 '13 at 22:34

3 Answers3

1
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isdigits(char *s){
    //return value : true if the string is all numbers.
    while(*s)
        if(!isdigit(*s++))
            return 0;
    return 1;
}

int main(void){
    char dateOfBirth[7];
    int len;
set4:
    printf("Date of Birth (DDMMYY)  : ");
    //Doesn't accept input more than specified number of characters
    fgets(dateOfBirth, sizeof(dateOfBirth), stdin);
    rewind(stdin);//keyborad buffer flush
    //fflush(stdin);//discard the character exceeding the amount of input
    //How fflush will work for stdin by the processing system (that is undefined)
    //while ('\n' != fgetc(stdin));//skip if over inputted
    len = strlen(dateOfBirth);
    if(dateOfBirth[len-1] == '\n') dateOfBirth[--len] = '\0';//newline drop
    if(len != 6 || !isdigits(dateOfBirth)){
        printf("\nSorry, input is invalid\n");
        goto set4;
    }

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

isalpha expects an int not a char * (pointer). You should loop over the string and validate the characters individually:

for(int i = 0; i < strlen(name); i++){
    if(!isalpha(name[i])){
        /* errors here */
    }
}

Also: goto's are bad!. So is gets, use fgets instead.

Community
  • 1
  • 1
Kninnug
  • 7,992
  • 1
  • 30
  • 42
0

Check manpage of isalpha.. Its expects int as argument.

To know whether user input is valid name or not, create your own function as,

/* a-z and A-Z are valid chars */
int isValidName( char *str )
{
  if( str == NULL )
  {
    return 0;
  }

  while( *str )
  {
    if( ! isalpha( *str ) ) 
    {
      return 0;
    }
    str++;
  }
  return 1;
}
VoidPointer
  • 3,037
  • 21
  • 25