none
Quit Script If User Does Not Enter Number in a Select Case Input RRS feed

  • General discussion

  • Hello Scripters,

    I am working on a utility with very little input from the user.  Mostly point and click.  Most the user would need to do is enter a number which will correspond to a string of text which will be used to generate the commandline further down into the script.

    I would like to introduce error handling which will end the script if the user enter anything else beside 1, 2 or 3.  I have tried Select Case Else with no success.

    I tried an example (excerpt below) found in the Microsoft WSH and VBScript Programming for the Absolute Beginner, 4th ed.

    Select Case strAnswer
      Case "rock"
        If intGetRandomNumber = 3 Then strResults = "Tie"
        If intGetRandomNumber = 2 Then strResults = "You Win"
        If intGetRandomNumber = 1 Then strResults = "You Lose"
      Case Else
        objWshShell.Popup "Sorry. Your answer was not recognized. " & "Please type rock, paper, or scissors in all lowercase letters."
        WScript.Quit
    End Select
    Thanks,


    Thanks, CAZan

    • Changed type Bill_Stewart Tuesday, November 21, 2017 6:49 PM
    • Moved by Bill_Stewart Tuesday, November 21, 2017 6:49 PM This is not "do my homework for me" forumn
    Monday, June 12, 2017 3:19 PM

All replies

  • Is this a homework question?

    -- Bill Stewart [Bill_Stewart]

    Monday, June 12, 2017 3:37 PM
  • where ever you get the input from the user, run the check there. I know you're working in VBScript but in PowerShell, you could run something like the below:

    do {$input = Read-Host "enter 1, 2 or 3"}while (("1","2","3") -notcontains $input)

    Hopefully, this gives you some inspiration :) 

    Monday, June 12, 2017 3:57 PM
  • Homework Question?

    Thanks, CAZan

    Monday, June 12, 2017 10:57 PM
  • Hi Mark,

    Unfortunately, I have to stick with the VBScript.  Everything else is working except for this.  I actually performed a check on the Input with an IF THEN for as little as "", no input.  That didn't work out.


    Thanks, CAZan

    Monday, June 12, 2017 11:01 PM
  • Your question and code make no sense.  Try asking a complete question or describe what you want to do in a way that makes sense.

    strNum = "2"
    num = CInt(strNum)
    
    if num >= 1 and num <= 3 Then
         ' good number
    Else
         ' bad number
    End If
    



    \_(ツ)_/


    • Edited by jrv Monday, June 12, 2017 11:17 PM
    Monday, June 12, 2017 11:16 PM
  • It was a broad question to prompt a variety of options.  I didn't want to be too specific.  I used an excerpt from a script referenced in a scripting book.  But, if you are having trouble following me on this, I'll try again.

    I wanted to know if anyone had worked on this type of method where they wanted the script to quit when the user didn't enter anything into the Input box.  It seems to work all right if anything else is entered besides 1,2 and 3 as in numbers.  Text and no characters seems to be ignored and it defaults to the first item, "MD5".


    Thanks, CAZan


    • Edited by CAZan Tuesday, June 13, 2017 12:00 AM
    Monday, June 12, 2017 11:50 PM
  • if strInput = "" Then Wscript.Quit 99


    \_(ツ)_/

    Monday, June 12, 2017 11:53 PM
  • Unfortunately, I have to stick with the VBScript.

    Why?


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 13, 2017 3:26 PM
  • I have restrictions in my environment, which allows more flexibility with VBScript.  I can do quite a bit with VBScript.  I did come across a method to use a .NET form with PowerShell.  It has a very nice lay out with radio buttons and such.  But, PowerShell is highly restricted due to GPOs running in the domain.

    What were your thinking as an alternate option.  Now, this question was only dealing with a smaller part of a larger script.  In this part of the script, I needed to create options which would ultimately become command-line options further down into the script.

    After doing some more research into what I was doing, I had a funny feeling I was running my test in the wrong place.  Needed to run it outside of the Select Case to remove any non-numeric characters and null entries.  So, below is what I came up with.

    strAnswer = InputBox(message,Title,Default) If Not IsNumeric(strAnswer) Then MsgBox Wscript.Quit ElseIf strAnswer = "" Then MsgBox Wscript.Quit End If Select Case strAnswer Case "rock" If intGetRandomNumber = 3 Then strResults = "Tie" If intGetRandomNumber = 2 Then strResults = "You Win" If intGetRandomNumber = 1 Then strResults = "You Lose"

    ' The other Case options for "paper" and "scissors" goes here but omitted it to save space. Case Else objWshShell.Popup "Sorry. Your answer was not recognized. " & "Please type rock, paper, or scissors in all lowercase letters." WScript.Quit End Selec


    Thanks, CAZan


    • Edited by CAZan Monday, June 19, 2017 10:12 PM
    Monday, June 19, 2017 10:09 PM