Re: How to make sure that a string is valid
- --- In email@example.com, "paradisejb" <jburman1860@...> wrote:
> -snip-snip snip snip
> Now, you can simplify this some, and still get same result.
> input name$
> name$ = upper$(trim$(name$))
> length = len(name$)
> for i = 1 to length
> value = asc(mid$(name$, i, 1))
> if value <65 OR value > 90 goto [help]:exit for
> next i
> print name$
> print "you entered an invalid name"
> goto [start]
> --end code
> Note: It's not good programming practice to exit a for...next loop with a goto statement before the loop completes the count. The proper way is to use EXIT FOR if you need to exit the loop early. But, I'm not 100% sure the :exit for that I've added will ever be seen by the program and therefore, might not be proper exiting. You should read about this in the help file. Someone else may be able to clear this up for us.Haris,
> -snip snip
Hope this does not confuse you. My use of exit for in the code sample is not correct. So, I am now 100% sure that the :exit for will not be seen by the program.
The part about exiting a loop with goto before the count is complete is correct - it should not be done. We have to find a different way to exit the loop properly. Let's do it like this:
name$ = upper$(trim$(name$))
length = len(name$)
for i = 1 to length
value = asc(mid$(name$, i, 1))
if value <65 OR value > 90 then invalid = 1 :exit for
if invalid = 1 then goto [help]
print "you entered an invalid name"
invalid = 0 'reset to 0 for next loop through
> --end code