loops - How do i remove NULL input in C? -
My teacher told me address me "idiot proof of abuse of any kind", so I have a program that
Here is the United Kingdom's foolproofed code
# include & lt; Stdio.h & gt; # Include & lt; Conio.h & gt; Int main () {four lessons [16]; Printf ("type something:"); Fgets (text, 16, stdin); Printf ("You typed:% s", text); getch (); } I made some simple adjustments to make sure there is no error, however, if I can not get the filter to work properly, because it's still zero Allows input
#include & lt; Stdio.h & gt; # Include & lt; Conio.h & gt; Int main () {four lessons [16]; Int loop; Do {printf ("Type something:"); Fgets (text, 16, stdin); If (text [0] == '\ 0') {printf ("try again"); System ("cls"); Loop = 1; } Else {loop = -1; }} While (loop> 0); Printf ("You typed:% s", text); getch (); } I have tried Google and I can not find a solid answer, it is probably some simple line of code, but unfortunately I do not know what this is.
Edit: This is fine, if the statement should be:
If (text [0] == '\ n')
fgets () using the return value from to I / O is the first phase of a fool-proofing the Four Lessons [16]; Printf ("type something:"); If (fgets (text, size, text, stdin) == faucet) {if (feof (stdin)) Handle_stdin_is_closed (); // no more input (if panic (stdin) Handle_IOerror (): // very rare occurrence, and more common} // test input files only a '\ n' if (text [0] == ' \ n '.) printf ( "Try again"); // look for long lines size_t len = strlen (text), if (lEN + 1 == sizeof text & amp; & amp ;! text [lane - 2] = '\ n') HandleLongLine (); .. Let's next step would assume to see scan error code for a long read Is errno = 0; four * endptr; long = strtol (text, & endptr, 10) ); If (errno) Handle_NumericOverflow (); if (text == endptr) Handle_InputIsNotNumeric (); while (Isspace ((unsigned char) * endptr)) endptr ++, if (* endptr = '\ 0') Handle_ExtraTextAfterNumber () ;! Although this is a lot of code, the best spin off for the strong handling of the hostle user input is an assistant function where lots of tests can be done for
four * fast = "type something:"; long number; Integer State = GetLong (stdin, speed, and number); / / Put all the tests here. If (stat> gt; 0) hand_com eFailure (); If (State and lieutenant; 0) handle_of (); Printf ("% ld \ n", number);
Comments
Post a Comment