|
Post by jarychk on Jun 28, 2019 5:31:04 GMT
I have no idea why this use of INSTR() would fail. needcharacters$="1234567890" for i0=1 to len(M$)
if instr(needcharacters$,mid$(M$,i0,1))=0 then
flaginputscheck=0
end if
next i0
for i1=1 to len(S$)
if instr(needcharacters$,mid(S$,i1,1))=0 then
flaginputscheck=0
end if
next i1
I studied the helpfile for MID$ and INSTR and also the sample code from B+, justbasiccom.proboards.com/thread/305/checking-specific-invalid-characters-string , and I thought I finally understood well enough, but the above code is failing. EDIT: Look a few posts down to see the two posts showing how solved.
|
|
|
Post by jarychk on Jun 28, 2019 6:13:44 GMT
Also, why does THIS one work, while the one I posted above fails?
needcharacters$="1234567890" flaggoodinput=1
input "give a string expecting whole number only. ";v$
rem mid$ and instr to check every input character for i=1 to len(v$) if instr(needcharacters$,mid$(v$,i,1))=0 then print "bad character was included." flaggoodinput=0 end if next i
if flaggoodinput=1 then print "nice input, whole number probably." end if
END
|
|
|
Post by Rod on Jun 28, 2019 6:59:34 GMT
In the first example flaggoodinput is set to 0 by default. In the second example you remember to set it positive prior to testing.
|
|
|
Post by jarychk on Jun 28, 2019 7:31:42 GMT
In the first example flaggoodinput is set to 0 by default. In the second example you remember to set it positive prior to testing. Thanks probably, Rod. I made two different example programs to study this. In the first example, the name of the flag was flaginputscheck, and initialization was to 0, as you observe. The second example, the name of the flag was flaggoodinput, and was initialized to 1, as you observe. The first example failed to run, stopping at the line of code using the INSTR() function. I will review them again in the JB editor and retry and rerun.
|
|
|
Post by jarychk on Jun 28, 2019 7:46:03 GMT
(SOLVED: See below after "Wait!")
No. This still fails, stopping at the line saying, if instr(needcharacters$,mid(S$,i1,1))=0 then
This is the example program:
needcharacters$="1234567890" flaginputscheck=1
input "give minutes. ";M$ input "give seconds. ";S$
for i0=1 to len(M$) if instr(needcharacters$,mid$(M$,i0,1))=0 then flaginputscheck=0 end if next i0
for i1=1 to len(S$) if instr(needcharacters$,mid(S$,i1,1))=0 then flaginputscheck=0 end if next i1
I do not know why it stops and fails. I have no idea what is wrong or what I miss.
SOLVED ----
... Wait! I think I see, for this example.. Type Mismatch. I will continue studying this...
|
|
|
Post by jarychk on Jun 28, 2019 7:53:09 GMT
SOLVED - The corrected code
This now seems good:
needcharacters$="1234567890" flaginputscheck=1
input "give minutes. ";M$ input "give seconds. ";S$
for i0=1 to len(M$) if instr(needcharacters$,mid$(M$,i0,1))=0 then flaginputscheck=0 end if next i0
for i1=1 to len(S$) if instr(needcharacters$,mid$(S$,i1,1))=0 then 'see now the include '$' symbol on BOTH mid$() functions. flaginputscheck=0 end if next i1
if flaginputscheck=1 then print "Inputs look good most likely." if flaginputscheck=0 then print "A wrong digit character happened."
END
|
|
|
Post by B+ on Jun 28, 2019 14:48:21 GMT
Here is an AllDigits Function (with test code) you can put in your JB Toolbox:
[getTime] input "Enter minutes > ";m$ if AllDigits(m$) then input "Enter seconds > ";s$ if AllDigits(s$) then print "Minutes and seconds are probably good." : goto [continue] else print "Seconds not good." end if else print "Minutes not good." end if goto [getTime] [continue]
function AllDigits(testString$) if len(testString$) then for i = 1 to len(testString$) if instr("0123456789", mid$(testString$, i, 1)) = 0 then exit function next AllDigits = 1 'true = not 0 end if end function
Use AllDigits test when ever you need whole positive number strings. val() should ignore all leading zero's.
|
|
|
Post by zzz000abc on Jul 14, 2019 17:15:11 GMT
1 needcharacters$="1234567890"
2 for i0=1 to len(M$)
3
4 if instr(needcharacters$,mid$(M$,i0,1))=0 then
5
6 flaginputscheck=0
7
8 end if
9
10 next i0
11 'S$=" "
12
13 for i1=1 to len(S$)
14 print "yes"
15
16 if instr(needcharacters$,mid(S$,i1,1))=0 then
17
18 flaginputscheck=0
19
20 end if
21
22 next i1
check the above code two mistakes are there at lines numbered 13 and 16 respectively. at 13: for i=1 to 0 wrong but for i=1 to 0 step -1 is correct. without initializing S$ you have used for loop so the control will not enter it. at 16:instead of mid$() you have written mid() maybe typing mistake. unquote the line 11 and check output print statement line 14 works.
|
|
|
Post by jarychk on Jul 14, 2019 21:08:44 GMT
zzz000abc Like I said, "SOLVED". The mistake was the type-mismatch on mid(), NEEDING TO BE MID$(). That fixed everything. The basic method is now in use in a well-working program.
|
|