Welcome !

if input equals code then msgbox "correct"

Author Message

  • Total Posts : 1
  • Scores: 0
  • Reward points : 0
  • Joined: 12/17/2016
  • Status: offline
if input equals code then msgbox "correct" Saturday, December 17, 2016 4:29 AM (permalink)
[Helpful answer received] / [List Solutions Only]
Hey guys, I am very new to vbscript, and I am trying to write a script which will generate a random 5 digit number, display that code in a msgbox, and then ask for user input. It will then compare the entered code to the generated code, and if they match, make a msgbox "correct". I have made this script: 
Dim max,min,code,inCode
code = Int((max-min+1)*Rnd+min)
msgbox code
inCode = InputBox("Enter the 5 digit code","Enter Code")
If inCode = code Then msgbox "correct" Else msgbox "incorrect"
which works up to the if statement, where it will always be incorrect no matter what. I have also made this code:
InCode=InputBox("Enter the 5 digit code","Enter Code")
If InCode=12345 Then msgbox "correct" 
which works, so I think that the problem might be with generating the random number. please let me know that you think. Thanks, thepi

    • Total Posts : 2626
    • Scores: 253
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:if input equals code then msgbox "correct" Saturday, December 17, 2016 11:24 PM (permalink)
    [This post was marked as helpful]
    Your check always fails, because the variables have values of different (sub)types. An InputBox always returns a string, but the variable code contains an integer value. Change the value from the InputBox to an integer like this:
    If CLng(inCode) = code Then ...
    and the problem will disappear.

    Your code works when you have a literal value in it because VBScript handles literals differently from variables, as explained here. Basically, when you compare a string variable to an integer variable the string is always greater than the integer. If you compare a string variable to an integer literal the string is converted to an integer before comparing the values, so the comparison works as one would expect.
      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2017 ASPPlayground.NET Forum Version 3.9