Thread: Error checking for is number or isdigit?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Error checking for is number or isdigit?

    How would I go about checking for mishaps in typos that is suppose to be in a number format as such 3s or 3m or 3 if someone enters something like this,
    Code:
    userx@slackwhere\⚡/media/data/C-Projects/mhsetroot/mhsetroot/bin/Debug\⚡\ > 
    
    ./mhsetroot -over-ride   -z f3m  /media/data/wallpaper
    f3m //that is what was typed in
    0 // that is what what the value is in the var because nothing usable is returned.
    defaults time settings // that is what gets called if value is zero,
    therefore the function that sets the default time when -z option is not used is called due to var being zero.
    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);
    }
    just to show how it calls for the value using getopts, and the other function that sets default time when option -z not used. So you'll know what is going on with that end of it.
    Code:
      case 'z':
                if (optarg == NULL)
                printf("< %s >\n",optarg);
                else
                opts.ch_t =  seconds_or_minutes(optarg);
    
                printf("%d\n",opts.ch_t);
                break;
    
    -------------------------------------
    
    #ifndef DEFAULT_TIME
    #define DEFAULT_TIME (5 * 60)
    #endif // DEFAULT_TIME
    
    void default_time_settings(void)
    {
    printf("defaults time settings\n");
        if(opts.ch_t == 0)
            opts.ch_t = DEFAULT_TIME;
    }
    So, what would, is a good way to accomplish this feet (feat) of getting it to stop the program if such a mishaps as that is preformed?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,063
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error checking for is number?
    By userxbw in forum C Programming
    Replies: 7
    Last Post: 09-22-2017, 05:30 PM
  2. isdigit() to validate if input is number..
    By narendrav in forum C Programming
    Replies: 11
    Last Post: 06-16-2012, 12:16 PM
  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 for whole number
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:55 PM
  5. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM

Tags for this Thread