tags:

views:

171

answers:

6

I am writing a script where I need to make sure they only put in certain characters. These include "x", "/", "+", "-", "%" (basic math operators), every letter from a -z and every number. I have the following below that only checks for alpha and number. How can I check that only certain one are used, and everything else, such as "&" or ">", are correctly error handled?

//check to see if user has input an incorrect symbol or letter
    if (isalpha(symbol) || isalnum(symbol)) 
    {
     printf("You must enter a math operator, not a letter or number. \n \n");
    }
    else {//move along nothing to see here
    }
A: 

If it's a char, then you do something like this

if(charVariable == '+')

These need to be in single quotes.

Ólafur Waage
+3  A: 

Write your own isMathOperator function that returns true for that symbols you want to allow.

TheUndeadFish
+9  A: 

Make a string with all the allowed characters and then check the string.

char* ok = "/+-*%";

if (isalpha(symbol) || isalnum(symbol) || strchr(ok, symbol) == NULL) 
{
    printf("You must enter a math operator, not a letter or number. \n \n");
}
else {//move along nothing to see here
}
FigBug
On this, you are essentially giving an error because they are using the allowed characters. Should it be != strchr(ok,symbol)?
HollerTrain
strchr() returns NULL if the character is not found in the string. I have edit the answer to fix the error.
Artelius
I'm a little confused to what he wants, his code seems the opposite of what he asked. If he just wants to test for not an operator it should be:if (!strchr(ok, symbol)){}The current answer doesn't need the isalpha() or isalnum().Mind you, x is both a letter and an operator. * should probably be used instead.
FigBug
+1  A: 

I think you have to check every input character by itself. strchr can help

/* code untested. I don't have a compiler available at the moment */
/* input = "123 / 14 + x - 5"; */
char *pinput = input;
while (*pinput) {
    if (!strchr("+-*/% abcdefghijklmnopqrstuvwxyz0123456789", *pinput)) {
        /* unacceptable character */
        break;
    }
    ++pinput;
}
if (*pinput != '\0') {
    fprintf(stderr, "Invalid input\n");
}
pmg
+1  A: 

The general answer to this kind of question in C is that you do what would be done behind the scenes in a language with elaborate string handling: you examine each character and process it in open code.

Having said that, there are now two ways to process each character:

  • use a multi-way if or index a string of valid characters, probably with strchr(3)
  • use a lookup table, i.e., x['a'] = 1, if(x[i]) ...

And having said that, there is a hybrid approach which uses a preconstructed lookup table that is part of every C library since before C89, called ctype.h. The man pages for this are found under isalpha(3), use man 3 isalpha on unix and google or msdn if under windows.

DigitalRoss
A: 

Implementing the idea of TheUndeadFish:

int isMathOperator(int c)
{
    static char symbols[257] =
    {
        ['+'] = 1, ['-'] = 1, ['/'] = 1, ['x'] = 1,
        ['='] = 1, ['%'] = 1, ...
    };
    assert(c == EOF || (c & 0xFF) == c);
    return((c == EOF) ? 0 : symbols[c]);
}

Note that like the isxxxx() macros/functions in <ctype.h>, this function accepts any valid 8-bit character or EOF. It uses the C99 mechanism for initializing specific elements of an array.

Jonathan Leffler