Need help with validating an input.

This is a discussion on Need help with validating an input. within the C Programming forums, part of the General Programming Boards category; Description: Calculates the integral between 0 and a user defined limit. Problem: I can't seem to get the validation of ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Need help with validating an input.

    Description: Calculates the integral between 0 and a user defined limit.

    Problem: I can't seem to get the validation of temp to work.


    cos2.dat:
    Code:
    0.000000  1.000000
    0.000157  1.000000
    0.000314  1.000000
    0.000471  1.000000
    0.000628  1.000000
    0.000785  0.999999
    0.000942  0.999999
    0.001100  0.999999
    a         0.999998 // test fail point.
    0.001414  0.999998
    0.001571  0.999998
    0.001728  0.999997
    0.001885  0.999996
    0.002042  0.999996
    ....
    Program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    
    #define max_limit 1.570639
    #define limitcheck(x) ((x < 0)||(x > max_limit) ? -1 : x)
    
    /* Function prototype  */
    double integral(double x[], double f[], int N, double *a, double *b);
    
    int  main(int argc, char* argv[])
    {
    	FILE		*input;
    	int		i=0, N=0, v1=0, v2=0;	
    	double		a=0, b=0, valid[1], temp, dump, *x, *f, m;
    	const char	inp_fn[]="cos2.dat";
    	char		test;
    	 
    	 if(argc != 2) // Check input.
    	 {
    		 printf("You have failed to provide the integration limit\n");
    		 exit(EXIT_FAILURE);
    	 }
    	 else // Read input.
    	 {
    		 valid[0]=sscanf(argv[1], "%lf", &b);
    		 
    		 b = limitcheck(b); // Check the limit is within range.
    		 
    		 /* Open input file */
    		 input = fopen(inp_fn, "r");
    		 
    		 if(!valid[0]) // Invalid input.
    		 {
    			 printf("You have failed to provide a valid integration limit.\n");
    			 exit(EXIT_FAILURE);
    		 }
    		 
    		 else if(b < 0) // Invalid range.
    		 {
    			 m = max_limit;
    			 printf("Please enter integration limit between 0 and %lg.\n", m);
    			 exit(EXIT_FAILURE);
    		 }
    		 
    		 else if(input == (FILE*) NULL) // No file.
    		 {
    			 printf("Input file could not be opened (cos2.dat).\n");
    			 exit(EXIT_FAILURE);
    		 }
    		 else // Valid.
    		 {
    			 for(N=0; (b>temp); N++)
    			 {
    				fscanf(input, "%lf", &temp);
    				fscanf(input, "%lf", &dump);
    				
    				 if( isalpha(temp) )
    				 {
    					printf("Input file was corrupted (cos2.dat).\n");
    					exit(EXIT_FAILURE);
    				 }
    			 }
    			 rewind(input); // Reset to begining of file.
    			 
    			 x = malloc((N)*sizeof(double)); // Allocated memory for x.
    			 f = malloc((N)*sizeof(double)); // Allocated memory for f.
    			 
    			 for(i=0; i<(N-1); i++) // Inputing array values.
    			 {
    				fscanf(input, "%lf", &x[i]);
    				fscanf(input, "%lf", &f[i]);
    			 }
    			 
    			 /* Close input file */
    			 fclose(input);
    			 
    			 integral(x,f,N,&a,&b);
    			 
    			 free(x); // Clearing memory
    			 free(f); // Clearing memory
    			 exit(EXIT_SUCCESS);
    		 } 
         }
         return(0);
    }
    
    double integral(double x[], double f[], int N, double *a, double *b)
    {
    	int		j=1;
    	double	sum=0, h=0;
    	
    	/* Calculating integral */
    	for (j=1; j <= (N-2); j= j+1)
    	{
    		sum += f[j-1]*(x[j]-x[j-1]);
    		
    		h = (x[j]-x[j-1]);	
    	}
    	
    	sum += f[j-1]*(*b-x[j-1]); // Adding final term.
    	
    	printf("Integral of cos^2 between %lf and %lf = %lf\n", *a, *b, sum); // Output
    	
    }
    Last edited by morrjame; 12-17-2010 at 12:19 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Check your return value from fscanf() to see if you got any data.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fscanf() returns the number of input items successfully matched.
    Why are you assigning it to double type?
    What's the point of array of one element?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    temp was not initialized before you do for(N=0; (b>temp); N++)
    Also, are you sure the way the file is ended is to see a value that is not less than b?
    In your next loop are you sure you are going far enough? You allocated for N elements but are only inputting N-1.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    @Dino
    I checked and I do get the data out.

    @Bayint Naung
    I don't think I have set fscanf() to double, and I made valid[] an array in case I needed to add more for more arguments.

    @nonoob
    I tried with temp initialized but the validation still didn't work.
    I dont how to make the way the file is ended is to see a value that is not less than b, I just made the max_limit equal to the largest value in the file.
    In the assignment it said to make the integral up to but never higher then the user defined limit.me
    I changed the memory allocation to N-1 but I doubt it makes much difference.

    Thanks for your help, but the validation still doesn't work, is it a problem with my use of the isalpha function.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think the point is, since you are attempting to read a double, scanf will simply not read in a character. If there is one in your file, well, then you're stuck -- scanf was not the right function to use. If you need to do input validation, you must suck in the data using something like fgets, and then see whether that data is what you want it to be.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,628
    Look at the docs for fscanf and isalpha. You need to check the return value for fscanf, as it will report 0 conversions when you find a character in your input file. Since you don't check this, temp and dump still have their values from the previous fscanf calls, and your loop never terminates.

    If you might come across text that you have to examine, try reading a whole line from the file using fgets into some buffer, check the first char of that buffer with isalpha, then parse the line with a single sscanf call. You still need to initialize temp.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,628
    And yes, you are misusing isalpha. It takes an int and returns an int, no doubles allowed.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Thanks for all the help, I learnt a lot whilst making this. I think it could be simpler, so any suggestions are welcome, but at least Iíve got it working. Have a happy Christmas, James.

    Code:
    /* Validation of data */
    for(N=0; (b>temp); N++)
    {
    	fgets(buf, 64, input);
    	buflen = (int)strlen(buf);
    	strncpy (line,buf,buflen-1);
    	token = strtok (line," .");
    	
    	for (ttotal=0; (token != NULL); ttotal++)
    	{
    		tlength = (int)strlen(token);
    		
    		for(n=0; n<(tlength); n++)
    		{
    			if( isdigit(token [n]) ); 
    			else goto Data_corruption;
    		}
    		token = strtok (NULL, " .");
    		n=0;
    	}
    	
    	if (ttotal != 4)
    	{
    		Data_corruption:
    		printf ("Data corruption on line %d.\n", N+1);
    		goto exit_failure;
    	}
    	else
    		temp = strtod (buf, NULL);
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just do:
    Code:
    fgets( ... );
    if( sscanf( buf, "%f %f", &f1, &f2 ) != 2 )
    {
        printf("Input error. Got: \'%s\'\n", buf );
    }
    else
    {
        ...success, you have 2 floating point numbers stored in f1 and f2 now
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  4. Validating Monetary Input
    By Hexxx in forum C++ Programming
    Replies: 7
    Last Post: 02-01-2006, 08:27 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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