Loading ...
Sorry, an error occurred while loading the content.

Re: How to make sure that a string is valid

Expand Messages
  • paradisejb
    ... -snip snip snip ... Haris, 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
    Message 1 of 4 , Mar 7, 2011
    • 0 Attachment
      --- In libertybasic@yahoogroups.com, "paradisejb" <jburman1860@...> wrote:
      > -snip
      >
      > Now, you can simplify this some, and still get same result.
      > --code
      > [start]
      > 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$
      > end
      > [help]
      > print "you entered an invalid name"
      > goto [start]
      > --end code
      >
      -snip snip snip

      > 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.
      > -snip snip

      Haris,

      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:

      --code
      [start]
      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 then invalid = 1 :exit for
      next i
      if invalid = 1 then goto [help]
      print name$
      end
      [help]
      print "you entered an invalid name"
      invalid = 0 'reset to 0 for next loop through
      goto [start]
      > --end code
    Your message has been successfully submitted and would be delivered to recipients shortly.