validate numerical entry

This is a discussion on validate numerical entry within the C Programming forums, part of the General Programming Boards category; Hi is there any function/command to validate numberical entries, like it check to make sure the entered value is a ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    validate numerical entry

    Hi

    is there any function/command to validate numberical entries, like it check to make sure the entered value is a number?
    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Yup. Several are presented in the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    what am i looking for?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The FAQ is searchable; this is a search of it for "validate".
    http://faq.cprogramming.com/cgi-bin/...arch&match=all
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    isint there any simple command like strcmp or something like that? do i could say
    while(input!<"some command")
    bla bla

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, you could, but it likely wouldn't compile . . .

    If you're still talking about numerical entry, then perhaps you mean something like this:
    Code:
    while(scanf("&#37;d", &x) == 1 && x != some_command)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    but how do i say if its a character, then reinput?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You'll need to clear the offending character(s) from the input buffer and try to read another number. For example:
    Code:
    int get_number() {
        int r, x, c;
    
        for(;;) {
            printf("Enter a number: ");
            if((r = scanf("&#37;d", %x)) != 1) return x;
            else if(r == 0) {
                while((c = getchar()) != '\n' && c != EOF);
            }
            else {
                return INT_MIN;
            }
        }
    }
    Note that my code just eats all characters until a newline. You could, for example, eat all non-digit characters if you wanted to:
    Code:
    while((c = getchar()) != EOF && c != '\n') {
        if(!isdigit(c)) {
            ungetc(stdin);
            break;
        }
    }
    Something like that. There are better ways to do it, of course. For example, you could read a whole line of input with fgets() and then use sscanf() to parse that line.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    iv done this:
    Code:
    do
    {
    fprintf(output,"enter number");
    fscanf(input,"&#37;f",array[i]);
    }
    while(fscanf(input,"%f",&array[i]) != 1);
    But as you can probably see it will keep repeating like the matix or something lol, how could i fix that?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're calling fscanf() twice. (Once with array[i], once with &array[i]. ) You want to call it only once. And when it returns something other than 1 (if you have one format specifier), then you have to return whatever characters caused it to fail from the input stream. (Checking for EOF is a good idea, too.)

    A simple example?
    Code:
    for(;;) {
        fprintf(output,"enter number: ");
        if(fscanf(input,"&#37;f", &i) == 1) break;
        while(getchar() != '\n');
    }
    It has very little error checking, though. I'd definitely check for EOF if I were you, or you'd get another infinite "matrix" loop for another reason.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    but i dont understand the for(;?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Oh, that's just a forever loop. It's the same as while(1). I just prefer it, because, in the eternal words of a CBoard member, "(;;) is a secret code word for 'ever'."
    Last edited by dwks; 10-07-2007 at 02:41 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    hmmm so how do i still implement that in my code, i typed that in but get an error?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well . . . you shouldn't. This works just fine for me:
    Code:
    #include <stdio.h>
    
    int main() {
        double n;
    
        for(;;) {
            printf("Enter number: ");
            if(scanf("&#37;lf", &n) == 1) break;
            while(getchar() != '\n');
        }
    
        printf("Number: %f\n", n);
    
        return 0;
    }
    Can you post the code that fails to compile, or some of it at least?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Code:
    
    #include <stdio.h>
    #define SIZE 10
    
    FILE * output =NULL;
    FILE * input =NULL;
    
    // Procedure
    void display(float array[SIZE])
    {
    int i;
    	for(i=0; i<SIZE; i++)
    	{
    		fprintf(output,"&#37;.4f ",array[i]);// Display array
    	}
    	fprintf(output,"\n");
    }
    
    // Function
    float max(float array[SIZE])
    {
    	int i;
    	float max=0.0f;
    	for(i=0; i<SIZE; i++)
    	{
    		// For each number, the number is compared with max and if > it stores as max
    		if(array[i]>max)
    		{
    			max=array[i];
    		}
    	}
    	// Return the maximum integer
    	return max;
    }
    
    // MAIN program
    int main(int argc, char * argv[])
    {
    output = stdout;
    input = stdin;
    
    int i;
    float array[SIZE];
    
    	for(i=0; i<SIZE; i++)
    	{
    		do
    		{
        		fprintf(output,"Enter value %i: ",i);
        		fscanf(input,"%f",&array[i]);
    		}
    		while(fscanf(input,"%f",&array[i]) != 1);
    
    	}
    
    	fprintf(output,"\n");
    	fprintf(output,"Your values you entered are as follows:\n");
    	display(array);
    	fprintf(output,"The maximum value entered is:\n");	
    	fprintf(output,"%.4e\n",max(array));
    }

    here it is

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  3. Replies: 15
    Last Post: 01-11-2008, 02:57 PM
  4. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 07:30 AM
  5. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM

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