Hi
is there any function/command to validate numberical entries, like it check to make sure the entered value is a number?
thanks
Hi
is there any function/command to validate numberical entries, like it check to make sure the entered value is a number?
thanks
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.*
what am i looking for?
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.*
isint there any simple command like strcmp or something like that? do i could say
while(input!<"some command")
bla bla
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("%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.
but how do i say if its a character, then reinput?
You'll need to clear the offending character(s) from the input buffer and try to read another number. For example:
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:int get_number() { int r, x, c; for(;;) { printf("Enter a number: "); if((r = scanf("%d", %x)) != 1) return x; else if(r == 0) { while((c = getchar()) != '\n' && c != EOF); } else { return INT_MIN; } } }
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.Code:while((c = getchar()) != EOF && c != '\n') { if(!isdigit(c)) { ungetc(stdin); break; } }
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.
iv done this:
But as you can probably see it will keep repeating like the matix or something lol, how could i fix that?Code:do { fprintf(output,"enter number"); fscanf(input,"%f",array[i]); } while(fscanf(input,"%f",&array[i]) != 1);
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?
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.Code:for(;;) { fprintf(output,"enter number: "); if(fscanf(input,"%f", &i) == 1) break; while(getchar() != '\n'); }
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.
but i dont understand the for(;?
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.
hmmm so how do i still implement that in my code, i typed that in but get an error?
Well . . . you shouldn't. This works just fine for me:
Can you post the code that fails to compile, or some of it at least?Code:#include <stdio.h> int main() { double n; for(;;) { printf("Enter number: "); if(scanf("%lf", &n) == 1) break; while(getchar() != '\n'); } printf("Number: %f\n", n); return 0; }
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.
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,"%.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