Thread: Error evaluating 'char'

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Error evaluating 'char'

    I wrote a program that is supposed to evaluate a character that the user types in and tell the user if it's a number, a letter, etc.
    The problem is that the program always goes to 'else' and returns the wrong answer!

    Code:
    #include <stdio.h>
    
    int main()
    {
        int cod;
        char c;
    
        cod= (int) c;
    
        printf("Type one character");
        scanf("%c", &c);
        if (cod<=57 && cod>=48){
            printf("You typed a number\n");
        }else if (cod>=97 && cod<=122) {
            printf("You typed a letter\n");
        }else if (cod>=33 && cod<=46){
            printf("You typed a punctuation sign\n");
            } else {
            printf("What you typed was not a letter, a punctuation sign or a number\n");
            }
    return 0;
    }
    Thanks
    Alastor

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	printf("Type one character");
    	scanf("%c", &c);
    
    	if (cod <= '9' && cod >= '0')
    	{
    		printf("You typed a number\n");
    	}
    	// etc etc...
    No need to convert the char to an integer value or use cryptic "magic-number" ASCII codes.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    cod= (int) c;
    This does nothing at all. Perhaps if you actually did it after you read some value into c... Even if you do though, the cast is pointless.

    Next, there's nothing at all that says the numbers 48 - 57 have to represent numbers, or that 33 through 46 have to be letters, or that anything that doesn't fit into those ranges is punctuation. You should instead be using the functions included in <ctype.h> to check the value.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I was following the ASCII values Quzah, I tried another program before...

    Code:
    #include <stdio.h>
    
    int main()
    {
        int cod;
        char c;
    
        c='C';
        cod = (int) c;
       printf("C is %c and cod is %d", c,cod);
       }
    Since cod returned a numerical value (In ASCII of course) I thought that the program I posted before would work
    Thanks for the help!

    EDIT: Tonto, what if I want to check for letters?

    cod>='a' && cod<='z' ?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no guarintee you're using the ASCII character set. The C standard doesn't care anything about what character set you use. Thus, if you hard-code some numbers in, it may not work the way you expect if someone else (or you) compiles it elsewhere. While it may be sufficient for your simple program, you should try and genralize (IE: standardize) your code as much as possible.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Prefer:
    Code:
    isdigit(cod)
    isalpha(cod)
    To:
    Code:
    (cod >= '0' && cod <= '9')
    (cod >= 'a' && cod <= 'z')

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    so that's what isalpha is for...

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Of course, isalpha will include uppercase letters as well, so you'd actually want islower() if you just wanted lowercase.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM