r/QuickBasic • u/SupremoZanne • Nov 08 '24
In case anybody was wondering how write code to detect invalid ASCII characters for some projects.
/r/QBeducation/comments/1gmrzez/how_to_detect_invalid_characters_in_an_input/
1
Upvotes
2
u/CharlieJV13 Nov 09 '24
The flip-side approach (which some might find more useful depending on the scenario and/or how one is looking at the problem):
FOR c = 48 TO 56
z$ = z$ + CHR$(c) ' registers the valid characters (digits)
NEXT
DO
INPUT a$
IF a$ = "" THEN END
s = 1
FOR c = 1 TO LEN(a$)
s = s * INSTR(z$, MID$(a$, c, 1))
NEXT
IF s > 0 THEN PRINT a$
IF s = 0 THEN PRINT "all characters must be numeric digits."
LOOP
2
u/CharlieJV13 Nov 09 '24
For the giggles, another version that counts the number of input characters that are invalid: