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

Re: How to make sure that a string is valid

Expand Messages
  • paradisejb
    ... Hi, If your goal is just to check if all the characters in a name input by a user are valid letters, you are on the right track with this little program.
    Message 1 of 4 , Mar 6, 2011
    • 0 Attachment
      --- In libertybasic@yahoogroups.com, "Muhammad Haris" <imharis1985@...> wrote:
      >
      > Although it is a kind of plus point of liberty basic that you do not have to define variables on the start but at times this becomes a very very big headache.
      > For example here is another small code which I wrote:
      > Again the purpose of the code is to make sure that the name is valid (i.e no numerical values or symbols are used).
      > ----------------------------------------------------------------------------------------------------------------------------
      > [start]
      > INPUT name1$
      > name2$ = trim$(name1$)
      > name3$ = upper$(name2$)
      > length = len(name3$)
      > val1 = asc(left$(name3$,1))
      > val2 = asc(mid$(name3$,2,1))
      > val3 = asc(mid$(name3$,3,1))
      > val4 = asc(mid$(name3$,4,1))
      > val5 = asc(mid$(name3$,5,1))
      > if val1<65 or val1>90 goto [help]
      > if val2<65 or val2>90 goto [help]
      > if val3<65 or val3>90 goto [help]
      > if val4<65 or val4>90 goto [help]
      > if val5<65 or val5>90 goto [help]
      > print name3$
      > end
      > [help]
      > print "you entered an invalid name"
      > goto [start]
      > ----------------------------------------------------------------------------------------------------------------------------
      > Now I have a little grasp on the asc values but another problem has rises. This code is only going to work if the user enters a five letter string. Any letter more or any letter less and this code will become useless. I can use the length function to find the length of the name3$ but how am I suppose to write a code which can work no matter what's the length of name.
      > I thought doing it will be possible by using looping but I have no idea how to use them.
      > Any help will be greatly appreciated.
      >

      Hi,

      If your goal is just to check if all the characters in a name input by a user are valid letters, you are on the right track with this little program. All you need now is a looping structure - as you have said.
      --code (watch for line wraps)
      [start]
      input name1$
      name2$ = trim$(name1$)
      name3$ = upper$(name2$)
      length = len(name3$)
      dim value(length)
      for letter = 1 to length
      value(letter) = asc(mid$(name3$, letter, 1))
      next letter
      for num = 1 to length
      if value(num) <65 OR value(num) > 90 then goto [help]:exit for
      next num
      print name3$
      end
      [help]
      print "you entered an invalid name"
      goto [start]
      --end code

      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

      If you want to convert the name so that the first letter is upper case and the rest are lower case, then you have more work to do.

      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.

      Jack
    • 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 2 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.