0

Need to write a program to take input from a user in the form of an int and validate that what they entered is not a char from a-z.

Is there another way to do this other than:

if((num != 'a') && (num != 'b') && (num != 'c') && (num != 'd') etc.....) printf("You entered %d", num);

KThund
  • 49
  • 1
  • 3
  • 7
  • You should be care of the negative number which has `-` as a character ahead. – Tim Mar 17 '14 at 03:04
  • http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c – Liam M Mar 17 '14 at 03:05
  • Your question is ambiguous. It appears that you might be able to use: `int num; if (scanf("%d", &num) == 1 && !islower(num)) printf("You entered %d\n", num);` (noting the newline in the output). If the input is inherently non-numeric, the `scanf()` will fail; if the input is numeric but in the range of lower-case letters (97..122 in codesets derived from ISO 8859), then it will not be accepted. If you're using `int num = getchar();` to read a single character, the answer is somewhat different. – Jonathan Leffler Mar 17 '14 at 03:12
  • 1
    Accept the answer and set thy free. – aghoribaba Mar 17 '14 at 03:13

4 Answers4

4

Yes, use the isdigit() function provided in the ctype.h header file

aghoribaba
  • 131
  • 1
  • 1
  • 11
1

You can use the isalpha() function in the "ctype.h" header. It returns true if it is a letter. So you could do something like:

if ( !isalpha() )
{
    // Do whatever
}

Here is a link to the documentation for more information.

Mio
  • 74
  • 3
  • 2
    Assuming he'd be fine with **anything** except numbers, which I kinda doubt, since he mentions `int`. `isdigit()` would be a better solution. – Saraph Mar 17 '14 at 03:07
  • @Saraph I originally was going to go with using that, but I re-read the question and must have interpreted it wrong. You are correct. – Mio Mar 17 '14 at 03:29
1

Rather than trying to work out that it's not a character, work out that it is a digit and discard the rest using the isdigit function from ctype like below.

#include <ctype.h>

...

if (isdigit(num)) {
  printf("You entered %d\n", num);
}

But this only works on single characters which is quite useless when you read in strings. So instead you could instead use the function sscanf. Like this.

int num;
if (sscanf(numstr, "%d", &num)){
  printf("You entered %d\n", num);
} else {
  printf("Invalid input '%s'\n", numstr);
}

Another option is to use the atoi function. But as I recall it doesn't handle errors and I find quite inferior to sscanf.

hookenz
  • 36,432
  • 45
  • 177
  • 286
-1
#include <stdio.h>
int main()
{
    char c;
    printf("Enter a character: ");
    scanf("%c",&c);
    if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
       printf("%c is an alphabet.",c);
    else
       printf("%c is not an alphabet.",c);
    return 0;
}
Alaeddine
  • 1,571
  • 2
  • 22
  • 46
  • The variable must be declared as an integer, not a char. – KThund Mar 17 '14 at 03:04
  • This answer wasn't very different from the solution OP originally proposed. – Saraph Mar 17 '14 at 03:05
  • Theoretically, your alphabetic check could capture non-alphabetic characters. If you were on an IBM mainframe that used EBCDIC, then there are non-alpha characters between the codes for `'a'` and `'z'` — and the difference `'z' - 'a'` is not 25 in EBCDIC. Use the `isalpha()` macro from ``. – Jonathan Leffler Mar 17 '14 at 03:05
  • @Saraph: this solution is a lot more succinct than 26 equality comparisons. – Jonathan Leffler Mar 17 '14 at 03:06
  • @JonathanLeffler Okay, I'm assuming OP would eventually find out about `<, >` operators. – Saraph Mar 17 '14 at 03:09