#1
  1. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    Check input type..


    Hi again guys :), I'm working on a program which takes a version number to desided which program to run, the program is stored in a dir like 1.5 or 2.0..

    Anyway I'm taking user input (as a string) and then useing it to create a path to an executable (which is run with the system() function). What I really need to do is check that the input is/looks like a float (as described above).

    :) any help or comments you could give me would be great!

    Thanks,
    Mark.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    This may not always do the expected, but it will handle errors gracelfully:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BUFSIZE 25
    
    int main(){
        float version;
        char inputBuffer[BUFSIZE], outputBuffer[BUFSIZE];
        int i, isValid, len;
    
        for (;;){//forever until break
            isValid = 1;
            printf("Enter a float value for version or control 'C' to exit program\n");
            if (!fgets(inputBuffer, BUFSIZE, stdin)) break;
            len = strlen(inputBuffer);
            if (len < 2){
                printf("Please enter a value\n");
                continue;
            }
            if (inputBuffer[len-1] != '\n'){
                printf("Too much input!\n");
                continue;
            }
            for (i=0; i<len-1; i++){
                if (!isdigit(inputBuffer[i])){
                    if (inputBuffer[i] != '.'){
                        printf("You have entered an invalid character (only digits and and the decimal are allowed)\n");
                        isValid = 0;
                        break;
                    }
                }
            }
            if (isValid){
                //you can check for valid versions here
                sscanf(inputBuffer, "%f", &version);
                //change precision if you want more for a version
                //just be sure you don't overflow your own buffer!
                len = sprintf(outputBuffer, "Version: %.2f\n", version);
                if (len >= BUFSIZE){
                    fprintf(stderr, "Help!  I have overflowed my buffer!\n");
                    fprintf(stderr, "Press return to exit.\n");
                    getchar();
                    exit(1);
                }
                printf("\t%s", outputBuffer);
            }
        }
    
        return 0;
    }
    Last edited by mitakeet; September 1st, 2003 at 10:54 AM.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    for(; ;);

    lol :)
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I wish the damn smilies were turned off by default!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo