Thread: Error checking for is number?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Error checking for is number?

    off the command line (Linux) when entering a number with an 's' or 'm' if someone mishaps and puts something like this in.

    Code:
    int seconds_or_minutes(char *what_is_it)
    {
      printf("%s\n",what_is_it);
        const char sec = 's';
        const char min = 'm';
        char str1[1+strlen(what_is_it)];
    
        strcpy(str1, what_is_it);
    
        if ( strrchr(what_is_it, sec) )
        {
            return (atoi( strtok(str1, "s") ));
        }
        if( strrchr(what_is_it, min) )
        {
            return (atoi(strtok(str1,"m")) * 60);
        }
        if (isdigit(what_is_it))
        {
            printf("is not a number\n");
            exit(1);
        }
        printf("%s",what_is_it);
        //defaults to seconds
        return atoi(what_is_it);
    }
    This function is to get secs or minutes via a token 's', or 'm' if not either it will default to seconds. But if fat fingers Freddy obvious hits a wrong key and hits a char first. isdigit is not working, or am I applying this wrong?



    Note:
    the default settings print out (in next code block) is showing it is going to the function call to set for default time setting when the -z is not used to by pass it. that function checks for zero on the var holding the value. if true set to

    Code:
    #ifndef DEFAULT_TIME
    #define DEFAULT_TIME (5 * 60)
    #endif // DEFAULT_TIME
    Using the -z option
    Code:
    userx@slackwhere\⚡/media/data/C-Projects/mhsetroot/mhsetroot/bin/Debug \⚡\ >
    
    ./mhsetroot -over-ride   -z f3m  /media/data/wallpaper
    f3m
    0
    defaults time settings
    as it does not stop the mishaps it sets to zero (0) then gets set to the main default time settings no matter what. How do I get it to stop from running because of mishaps like this?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Well what_is_it is a char pointer.
    what_is_it[0] is the first char.
    isdigit(what_is_it[0]) would test the first char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Well what_is_it is a char pointer.
    what_is_it[0] is the first char.
    isdigit(what_is_it[0]) would test the first char.
    nope didn't work:

    Code:
    dia/data/wallpaper
    main 1
    seconds or minutes in comming w3m
    in getopts 0 // comes back zero
    
    change time 300 // gets set to function calling to set default
    Code:
    int seconds_or_minutes(char *what_is_it)
    {
     printf("seconds or minutes in comming %s\n",what_is_it);
        const char sec = 's';
        const char min = 'm';
        char str1[1+strlen(what_is_it)];
    
        strcpy(str1, what_is_it);
         if (isdigit(what_is_it[0]))
        {
            printf("is not a number\n");
            exit(1);
        }
    
        if ( strrchr(what_is_it, sec) )
        {
            return (atoi( strtok(str1, "s") ));
        }
        if( strrchr(what_is_it, min) )
        {
            return (atoi(strtok(str1,"m")) * 60);
        }
    
    
        printf(" seconds or minutes outgoing not return %s\n",what_is_it);
    
        //defaults to seconds
        return atoi(what_is_it);
    }
    this worked though
    Code:
      if (isdigit(what_is_it[1]))
        {
            printf("is not a number %s\n",what_is_it);
            exit(1);
        }
    results
    Code:
    main 1
    seconds or minutes in comming w3m
    is not a number w3m <-- caught it

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Well what_is_it is a char pointer.
    what_is_it[0] is the first char.
    isdigit(what_is_it[0]) would test the first char.
    what I posted earlier is not up yet, but it is still not correct.
    isdigit(what_is_it[0])
    did'dent work

    the isdigit(what_is_it[1])
    and only works 1/2 time no good, nope.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Well you need a better approach if you expect to pick apart garbage input from the user.

    3
    w3m
    3m
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Well you need a better approach if you expect to pick apart garbage input from the user.

    3
    w3m
    3m
    yeah, I'm a bit of a perfectionist about somethings.

    So I read on line that a char *var is just a zero based array of character(s) first letter is 0, second letter 1, so on and so fourth. but it see here it is not, 0 = the entire word (string).

    using [1] to check, on two different means of typing it, wrong and right.
    Code:
    > ./mhsetroot -t n -z s1m /media/data/wallpaper
    what is it[0] s1m
    what is it[1] 1m
    what is it[0] 115
    what is it[1] 49
    is not a proper format s1m
    check opts
    
    > ./mhsetroot -t n -z 11m /media/data/wallpaper
    what is it[0] 11m
    what is it[1] 1m
    what is it[0] 49
    what is it[1] 49
    is not a proper format 11m
    check opts
    using [0] to check, same
    Code:
    ./mhsetroot -t n -z s1m /media/data/wallpaper
    what is it[0] s1m
    what is it[1] 1m
    what is it[0] 115
    what is it[1] 49
    check opts
     
      > ./mhsetroot -t n -z 11m /media/data/wallpaper
    what is it[0] 11m
    what is it[1] 1m
    what is it[0] 49
    what is it[1] 49
    is not a proper format 11m
    check opts
    where the zero element is the entire string, and it looks like their is no way to actually check the first letter(char) of a string using this method.

    and none of this hair ball stuff I just read online works either, along with whatever else I just over wrote,
    Code:
       char fst; // = strdup(what_is_it);
        fst = what_is_it[0];
        printf("fst %d\n", fst);
        printf("fst  %s\n", &fst);
        //if (isdigit(what_is_it[0]))
        if (isdigit(fst))
    by the way that reads it is backwards to what I have been reading that it stores each single letter in an element separately, from what I have read and seen the examples showing this,
    Code:
    char *str = "noway"
    str[0] == n
    srt[1] == o
    str[2] == w
    etc...
    this is in no way is the truth.

    doesn't fegts or gets chomp off one character at a time?
    Last edited by userxbw; 09-15-2017 at 06:00 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    So is "s1m" valid or not?

    Is anything other than this valid?
    [0-9]+(s|m)?

    That is, 1 or more digits with an optional 's' or 'm' following.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    So is "s1m" valid or not?

    Is anything other than this valid?
    [0-9]+(s|m)?

    That is, 1 or more digits with an optional 's' or 'm' following.
    the format is num+indicator | num & ! indicator

    but as pointed out I was being overly picky with fat finger syndrome.
    I ended up with this if only a 's' or 'm'
    Code:
    int seconds_or_minutes(char *what_is_it)
    {
        const char sec = 's';
        const char min = 'm';
        char str1[1+strlen(what_is_it)];
    
        strcpy(str1, what_is_it);
        // if only s or m is present then return -1
        if ( ( ( strrchr(what_is_it, sec) != NULL ) || (strrchr(what_is_it, min) != NULL) ) && strlen(what_is_it) == 1)
        {
            return -1;
        }
        else if ( strrchr(what_is_it, sec) )
        {
            return (atoi( strtok(str1, "s") ));
        }
        else if( strrchr(what_is_it, min) )
        {
            return (atoi(strtok(str1,"m")) * 60);
        }//defaults to seconds
    
        return atoi(what_is_it);
    }
    Imputation
    Code:
       case 'z': //sets timer time
                if (optarg == NULL)
                    printf("< %s >\n",optarg);
                else
                    if (seconds_or_minutes(optarg) != -1)
                    {
                        opts.ch_t = seconds_or_minutes(optarg);
    
                    }
                    else
                    {
                        set_oops_color();
    
                        warning_message();
                    }
                break;
    Last edited by userxbw; 09-22-2017 at 05:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking if input is a number
    By Median in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2012, 10:42 AM
  2. Checking Peterson Number
    By sunny` in forum C Programming
    Replies: 12
    Last Post: 03-02-2012, 11:31 AM
  3. Checking for a number
    By a.mlw.walker in forum C Programming
    Replies: 7
    Last Post: 04-20-2009, 03:42 PM
  4. Checking number of words
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 07-11-2003, 02:13 PM
  5. checking for whole number
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:55 PM

Tags for this Thread