Home > Back-end >  C language how to realize the input of rigorous inspection
C language how to realize the input of rigorous inspection

Time:12-11

Let the student management system, how to judge the user input the wrong, let him enter again?
While (the scanf (" % d ", & amp; Num==0)
{
Printf (" format error, please enter again: ");
fflush(stdin);
}
This kind of writing can't check his Numbers as the beginning of the random input, such as: 15 DQW $@ # this, o great god to give a solution to the rigorous check his input

CodePudding user response:

Try using a regular expression, the digital \ d

CodePudding user response:

Use getchar (), one by one analysis, found illegal, will prompt,
If the legitimate, joining together own figures, string, etc.,

CodePudding user response:

Please refer to the "c + + console program to determine input digital"
Wish I could help you!

CodePudding user response:

Fyi:
//input two integers 
#include
Int main ()
{
Int a, b, n, v, r, k;
Char buf [80].
Char * p;

K=0;
R=EOF.
While (1) {
If (EOF==r) {
The fgets (buf, 80, stdin);
P=buf.
}
While (1) {
R=sscanf (p, "% d % n, & amp; V, & amp; n);
If (1==r) {k++; break; }
If (EOF==r) break;
p++;
}
If (EOF==r) continue;
P +=n;
If (1==k) a=v;
If (2)==k {
B=v;
break;
}
}
Printf (" % d, % d \ n ", a, b);
return 0;
}


 # include & lt; stdio.h> 
Char [] s="123 ab 4";
Char * p;
Int n, n, k;
Void main () {
P=s;
While (1) {
K=sscanf (p, "% d % n, & amp; V, & amp; n);
Printf (" k, v, n=% d, % d, % d \ n ", k, v, n);
If (1)==k {
P +=n;
{} else if (0==k)
Printf (" skip char [c] % \ n ", p [0]).
p++;
} else {//EOF==k
break;
}
}
Printf (" End. \ n ");
}
//k, v, n=1123,
//k, v, n=0123,
//the skip char []
//k, v, n=0123,
//the skip char [a]
//k, v, n=0123,
//the skip char [b]
//k, v, n=1,4,2
//k, v, n=1,4,2
//End.
  • Related