![]() |
| | #1 |
| Registered User Join Date: Feb 2009 Location: Seattle
Posts: 39
| Code not rejecting alphanumeric input. Should only accept alpha 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 | |
| | #2 |
| subminimalist 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 | |
| | #3 |
| Registered User Join Date: Feb 2009 Location: Seattle
Posts: 39
| 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 | |
| | #4 | |
| Registered User Join Date: Feb 2009 Location: Seattle
Posts: 39
| Quote:
| |
| MSF1981 is offline | |
| | #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 | |
| | #6 | |
| Registered User Join Date: Feb 2009 Location: Seattle
Posts: 39
| Quote:
Code: length - 1 | |
| MSF1981 is offline | |
| | #7 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
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 | |
| | #8 |
| Registered User Join Date: Feb 2009 Location: Seattle
Posts: 39
| |
| MSF1981 is offline | |
| | #9 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,363
| This results in undefined behaviour: Code: fflush(stdin); Code: gets(originalString); Code: int isalphaString(const char *str)
{
for (; *str != '\0'; ++str)
{
if (!isalpha(*str))
{
return 0;
}
}
return 1;
}
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 | |
![]() |
| Tags |
| alphanumeric, character, characters, input, isalpha |
| Thread Tools | |
| Display Modes | |
|
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 |