Validate digit or char!?

This is a discussion on Validate digit or char!? within the C Programming forums, part of the General Programming Boards category; char department; int done=0; do { printf("\nEnter Department:"); scanf("%s",&department); if(isdigit(department)) { if(department<1 || department>9) printf("\nError\n"); else { fprintf(fpold,"%s\n",department); done=1; } ...

  1. #1
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Unhappy Validate digit or char!?

    char department;
    int done=0;

    do
    {
    printf("\nEnter Department:");
    scanf("%s",&department);

    if(isdigit(department))
    {
    if(department<1 || department>9)
    printf("\nError\n");
    else
    {
    fprintf(fpold,"%s\n",department);
    done=1;
    }
    }
    else
    {
    printf("\nError (digit only)\n");
    }
    }while(!done);

    The above code is a part from my program to get data for a stock report. My department is meant for a digit between 1 and 9. Is the way I did correct? And, the above code will print Error if I enter a number between 1 and 9. Is there any other way to validate the user input data so that department only take digit between 1 and 9?

    Thanks in advance.
    It's unfulfilled dreams that keep you alive.

    //netboy

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    >if(isdigit(department))
    Looks good to me

    >if(department<1 || department>9)
    That should be if(department<'0' || department>'9')
    but another way would be if(!isdigit(department))
    or just use an else since if the first test is true then it is a digit and otherwise it's not so you can print the error.
    C code. C code run. Run code, run...please!

  3. #3
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Unhappy

    I tried. Adding a single quote (') to 0 and 9 doesn't work. Windows will said that I have performed an illegal operation when I key in a digit.

    Please help!

    Thanks in advance.
    It's unfulfilled dreams that keep you alive.

    //netboy

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's the working code, I changed the fprintf to printf so that I could debug so you will need to change that back.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
        char department; 
        int done=0; 
    
        do{ 
            printf("\nEnter Department:"); 
            scanf("%c",&department); 
    
            if(isdigit(department)){ 
                if(department <= '1' || department >= '9') 
                    printf("\nError\n"); 
                else{ 
                    printf("%c\n",department); 
                    done=1; 
                } 
            }else 
                printf("\nError (digit only)\n");
        }while(!done);
    
        return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Unhappy

    Mr Prelude! The code you wrote will printf 8 if I enter 89!
    Is there any better way to validate the data rather than using isdigit? All I want is o prevent user from keying char and a digit outside 1 and 9...

    Thanks in advance.
    It's unfulfilled dreams that keep you alive.

    //netboy

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, then what you want requires a totally different plan. You need to manually parse each element of an array and see if it's a digit, if it is then you print it and if it isn't you break. I've written a little template for you to use, edit it however you need to to get the proper results.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
        char department[10];
        int done = 0, i = 0; 
    
        while(!done){ 
            printf("\nEnter Department:"); 
            fgets(department, sizeof(department), stdin); 
    
            while(isdigit(department[i])){
                printf("%c",department[i]);
                ++i;
            }
            printf("\n");
            done = 1;
        }
    
        return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    22

    Hey same ?

    hey are you from TARC? perlude ?
    ______________________
    Wut ?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What is TARC?

    -Prelude
    My best code is written with the delete key.

  9. #9
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Cool

    Thanks Prelude! I got it already!
    I added another line so that it will print Error messsage when the user key in digit <0 || >9

    Thanks so much!
    It's unfulfilled dreams that keep you alive.

    //netboy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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