Code not rejecting alphanumeric input. Should only accept alpha

This is a discussion on Code not rejecting alphanumeric input. Should only accept alpha within the C Programming forums, part of the General Programming Boards category; This program is only supposed to accept alpha characters ie ABCD. But it is accepting alphanumeric input such as ABCD9. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Code not rejecting alphanumeric input. Should only accept alpha

    This program is only supposed to accept alpha characters ie ABCD. But it is accepting alphanumeric input such as ABCD9. What am I dong wrong?

    Code:
    void A_getOriginalString()
    {
    	if (!(isalpha(*originalString))) /* Why does work for 5554 but not for ABCD9? */
    	{
    		puts("Letters only please. Try again.\n");
    		fflush(stdin);
    		gets(originalString);
    	}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Test one character at a time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by MK27 View Post
    Test one character at a time.
    Duh!! I Thanks!

    This worked...

    Code:
    void A_getOriginalString()
    {
    	int i;
    	int length = strlen(originalString);
    	
    	for (i = 0; i < length; ++i)
    	{
    		originalString[i];
    	}
    
    	if (!(isalpha(originalString[i++])))
    	{
    		puts("Letters only please. Try again.\n");
    		fflush(stdin);
    		gets(originalString);
    	}
    }

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by MSF1981 View Post
    Duh!! I Thanks!

    This worked...

    Code:
    void A_getOriginalString()
    {
    	int i;
    	int length = strlen(originalString);
    	
    	for (i = 0; i < length; ++i)
    	{
    		originalString[i];
    	}
    
    	if (!(isalpha(originalString[i++])))
    	{
    		puts("Letters only please. Try again.\n");
    		fflush(stdin);
    		gets(originalString);
    	}
    }
    Oops! Perhaps this doesn't work. Now it's not accepting any string as valid input.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Okay, now I think I got it for realz....
    Code:
    	int i;
    	int length = strlen(originalString);
    	
    	for (i = 0; i < length; ++i)
    	{
    		originalString[i];
    		
    		if (!(isalpha(originalString[i])))
    		{
    			puts("Letters only please. Try again.\n");
    			fflush(stdin);
    			gets(originalString);
    		}
    		
    	}

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by MSF1981 View Post
    Okay, now I think I got it for realz....
    Code:
    	int i;
    	int length = strlen(originalString);
    	
    	for (i = 0; i < length; ++i)
    	{
    		originalString[i];
    		
    		if (!(isalpha(originalString[i])))
    		{
    			puts("Letters only please. Try again.\n");
    			fflush(stdin);
    			gets(originalString);
    		}
    		
    	}
    Okay maybe I had it for fakes. I think it should be
    Code:
    length - 1

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay maybe I had it for fakes. I think it should be
    Code:
    length - 1
    Nope. It's because you are pre-incrementing (as opposed to post-incrementing) i:
    Quote Originally Posted by MSF1981 View Post
    Code:
    	int i;
    	int length = strlen(originalString);
    	
    	for (i = 0; i < length; ++i)
    	{
    		originalString[i];
    		
    		if (!(isalpha(originalString[i])))
    		{
    			puts("Letters only please. Try again.\n");
    			fflush(stdin);
    			gets(originalString);
    		}
    		
    	}
    Use i++. And the line in red is meaningless/superfluous.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by MK27 View Post
    Nope. It's because you are pre-incrementing (as opposed to post-incrementing) i:

    Use i++. And the line in red is meaningless/superfluous.
    Ahh... Very cool! Thanks!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    This results in undefined behaviour:
    Code:
    fflush(stdin);
    This makes you vulnerable to a buffer overflow:
    Code:
    gets(originalString);
    I suggest that you write isalpha for a string, e.g.,
    Code:
    int isalphaString(const char *str)
    {
        for (; *str != '\0'; ++str)
        {
            if (!isalpha(*str))
            {
                return 0;
            }
        }
        return 1;
    }
    Now, in the loop where you request for the user to enter the string, read in the input using something safer like fgets(), then use isalphaString() to check if the input is correct. If it is, end the loop, otherwise print the error message and request for user input again.

    If you do use fgets(), then note that it places the newline in the string unless the string read is of the maximum length. Consequently, it may be useful to write a function to simplify the process of removing that newline, then replace the fgets() call with it, e.g.,
    Code:
    char *getLine(char *str, size_t size, FILE *fp)
    {
        if ((str = fgets(str, size, fp)))
        {
            char *new_line = strchr(str, '\n');
            if (new_line)
            {
                *new_line = '\0';
            }
        }
        return str;
    }
    Last edited by laserlight; 02-14-2009 at 11:24 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  3. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM
  4. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21