problem using isalpha

i am using isalpha function in my program but when i input any special character between the string it does not show any error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gotoxy(10,6);
cputs("Name \n");
gets(name);
for (a=0;a<=strlen(name)-1;a++)
{ x=0;
if(isalpha(name[a]))
      x=1;
  else
      x=0;
}
if(x==1)
goto i; // " goto i " goes ahead
else
if(x==0)
gotoxy(10,15);
cputs("#ERROR : Name cannot have digits or special characters \n");
getch();
Yes, youn need to break; the loop when x=0; (within the else case)
goto?
Your code depends on what is the last character in the string that is whether it is an alpha character or not.

You should rewrite your loop the following way

1
2
3
4
5
x = 1;
for ( a = 0; a < strlen( name ) && x; a++ )
{
   if ( !isalpha( name[a] ) ) x = 0;
}

It is supposed that an empty string "contains" alpha characters.


Also it would be better if the calculation of the string length would be placed outside the loop.

For example

1
2
3
4
5
6
len = strlen( name );
x = 1;
for ( a = 0; a < len && x; a++ )
{
   if ( !isalpha( name[a] ) ) x = 0;
}

Last edited on
Topic archived. No new replies allowed.