C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-14-2009, 06:40 PM   #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);
	}
}
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 06:49 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Test one character at a time.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-14-2009, 06:55 PM   #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);
	}
}
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 07:03 PM   #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.
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 07:10 PM   #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);
		}
		
	}
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 07:21 PM   #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
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 07:29 PM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-14-2009, 07:33 PM   #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!
MSF1981 is offline   Reply With Quote
Old 02-14-2009, 11:33 PM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,363
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;
}
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

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

Last edited by laserlight; 02-15-2009 at 12:24 AM.
laserlight is online now   Reply With Quote
Reply

Tags
alphanumeric, character, characters, input, isalpha

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie homework help fossage C Programming 3 04-30-2009 04:27 PM
<< !! Posting Code? Read this First !! >> biosx C++ Programming 1 03-20-2002 12:51 PM
Simple Code, looking for input. Alien_Freak C Programming 3 03-03-2002 11:34 AM
Who will map the scan code (inserted by VKD_Force_keys) to virtual key code? Unregistered Windows Programming 0 02-21-2002 06:05 PM
can anyone help me give a code about a program that will input positive numbers... vigen00 C Programming 1 10-01-2001 10:39 AM


All times are GMT -6. The time now is 05:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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