Thread: Rejecting float and string with int on the front

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    44

    Rejecting float and string with int on the front

    So here's my code, with already implemented check for input data.
    However, it doesn't reject float and eg. 3table as input for the size of a matrix.
    How to fix that?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    float determinant (float * data, int num);
    
    int error = 0;
    
    int main ()
    {
        int num, i;
        char temp[BUFSIZ];
        float *data;
        float *counter;
        float value;
        char *p; /*used in function strtol in order to find the end of the input number*/
        
        printf("Enter the size of matrix: ");
        
        do
        {
        scanf("%s", temp);
        num = strtol(temp, &p, 10);
        if (num > 10 || num < 1)
            {
            printf("matrix size must be integer from interval 1-10\n");
            }    
        }while(num > 10 || num < 1 || !(*p == '\n' || *p == '\0'));    
        
        
       printf ("Size of the matrix %d by %d\n", num, num); 
                        
              
        if (1) 
        {
            data = malloc(sizeof(*data) * (num * num));
    
            if (data != NULL) 
            {
                    for (counter = data, i = 0; counter < data + (num * num); counter++, i++) {
                    printf("Enter the %d element: ", (i + 1));
                    scanf("%s", temp);
                    *counter = atof(temp); 
            }
                
                value = determinant (data, num);
                free (data);
                
                if (!error)
                    printf("Value = %f\n", value);
            }
            else 
                printf("Memory allocation failed!\n\n");
        }    
        else
            printf("Invalid size of matrix\n");
        
        return 0;
    }
    
    float determinant (float * data, int num) {
        float result = 0;
        int i, j, place;
        float value = 1;
        float * counter, * new_data, * new_counter;
        
        if (num > 1) {
            for (place = 1; place < num + 1; place++) {
                new_data = malloc(sizeof(*new_data) * (num - 1) * (num - 1));
                if (new_data != NULL) {
                    for (counter = data, i = 1, j = 1, new_counter = new_data; counter < data + (num * num); counter++, i++) {
                        if (i % (num + 1) == 0) {
                            j++;
                            i = 1;
                        }
                        if (i == place || j == 1)
                            continue;
                        *new_counter = *counter;
                        new_counter++;
                    }
                    
                    value = (*(data + (place - 1))) * determinant(new_data, num - 1);
                    
                    if (error) {
                        free(new_data);
                        return 0;
                    }
                    
                    if (place % 2 == 0)
                        value *= -1;
                    
                    result += value;
                    free(new_data);
                }
                else {
                    printf("\nMemory allocation failed!\n");
                    error = 1;
                    return 0;
                }
            }
            return result;
        }
        return *data;
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    To do that you'll pretty much have to read the line in as a string, and parse it yourself. There's no easy way to do it unfortunately.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    one trick might be to use sscanf with %f instead of strtol to read the input. then if the number has a fraction, you can detect that and reject the number. detect it by casting to int then back to float and compare it to the original. if they aren't equal you got a value with a fractional part.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Robustly checking input is quite hard. strtod() is good enough, however. You pass an end pointer. If the end pointer doesn't point to the nul character on return, then there must have been trailing non-digits in your string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add to Front, Remove from Front, Traverse
    By Brandon Smith in forum C Programming
    Replies: 9
    Last Post: 04-10-2012, 03:26 PM
  2. Help with rejecting char's from integer only input
    By Arkio in forum C Programming
    Replies: 1
    Last Post: 03-24-2011, 06:28 PM
  3. Replies: 8
    Last Post: 02-14-2009, 11:33 PM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. Appending to the front of a string in a class...
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2002, 05:52 PM