VBscript help

Hello! I am taking a scripting for system admins class. My programming knowledge is novice level, but I am quite enjoying it. I am stuck and wondering if there might be any VBscript gurus who can help me with this assignment. I made a number guessing script, that tells you to guess a number between 1 - 1000. With simple greater than or less than I can tell the user, if their choice is too high or too low. But the instructor told us to devise a method to tell the user if they are 800, 600, 400, 200, 100 too high, and too low. Any thoughts?

'*************************************************************************
'Script Name: GuessANumber.vbs
'Author: Matt
'Created: 4/23/17
'Description: This script plays a number-guessing game with the user
'*************************************************************************

'Initialization Section

Option Explicit

Const cGreetingMsg = "Pick a number between 1 - 1000"

Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses, objWshShl, strTargetFolder, objDesktopShortcut, objProgramsShortcut, strAppDataPath

intNoGuesses = 0

'Main Processing Section

'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((1000 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"

'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1

'See if the user provided an answer
If Len(intUserNumber) <> 0 Then

'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then

  'Test to see if the user's guess was correct
  If FormatNumber(intUserNumber) = intRandomNo Then
    MsgBox "Congratulations! You guessed it. The number was " & _
      intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
      "in " & intNoGuesses & " guesses.", ,cGreetingMsg
    strOkToEnd = "yes"
  End If

  'Test to see if the user's guess was too low
  If FormatNumber(intUserNumber) < intRandomNo Then 
    MsgBox "Your guess was too low. Try again", ,cGreetingMsg
    strOkToEnd = "no"
  End If

  'Test to see if the user's guess was too high
  If FormatNumber(intUserNumber) > intRandomNo Then
    MsgBox "Your guess was too high. Try again", ,cGreetingMsg
    strOkToEnd = "no"
  End If      

Else
  MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If

Else
MsgBox "You either failed to type a value or you clicked on Cancel. " & _
"Please play again soon!", , cGreetingMsg
strOkToEnd = "yes"
End If

Loop

So you want the program to communicate if they are exactly 800, 600, 400, 200 or 100 too high or too low?

I'd set up an array/vector with the numbers and loop through it comparing each value to the difference you have between the random number and the guessed number. Else you'd need a bunch of if statements but this way the loop takes care of that.

Haven't touched vb in any form for ages so you write it :slight_smile:

Oh, and if the numbers can be expressed as a mathematical series you could generate the array but in this case it's just 5 values so maybe it makes less sense.

@caprica Yes exactly. A loop is heavily implied in the readings. My problem is coming up with the syntax. My initial thoughts would be something like:
If FormatNumber(intUserNumber) - intRandomNo = < 800 Then Msgbox "You are 800 too high" but that syntax is simply not right. How can you subtract the two variables to decide how many values away you are? I appreciate the input. An array might make sense, for each 800, 600, 400, 200, 100 High/Low. *Edit it doesnt need to tell if you are exactly those amount away, Just over/under 800, 600...etc... to help you narrow down your choices.

*Edit, here's the addition the instructor wants me to make. "Add another If/ElseIf/Else Condition to check for if the user is less than 800,600,400,200,100 or farther from the correct guess, but still too low or too high."

fuck it, I'll do this in C++.

You copy it over to VB.

Fucking hate VB but I want to help you out.

@Dynamic_Gravity Thank you for you're help either way! I guess you wouldn't have to write the whole thing, i'm just looking for the method of finding out the difference between user input, and the integer returned by the RandomNo, and then seeing if that is too high or too low. If that makes sense.

alright I'm done. Total time, 31 minutes.

Here's the source. Let me know if you need comments.

EDIT: I added comments to help you understand it better.

If I can do this in 30 minutes you should have no problems porting this to VB, good luck!

Also, if you have any questions, ask away!

Drinking a bunch of rum right now so my coding skills are l33t.

@Dynamic_Gravity That converted beautifully over to VB. And I parsed through it to find the method you used, or it thought you used. Which turned out to be:

Dim diff As Integer = ran - in

	If diff > 0 Then
			Select Case diff
			Case 800
				Console.Write("800 too low" & vbLf)
			Case 600
				Console.Write("600 too low" & vbLf)
			Case 400
				Console.Write("400 too low" & vbLf)
			Case 200
				Console.Write("200 too low" & vbLf)
			Case 100
				Console.Write("100 too low" & vbLf)
			Case Else
				Console.Write("too low" & vbLf)
			End Select
	Else
			Select Case diff
			Case -800
				Console.Write("800 too high" & vbLf)
			Case -600
				Console.Write("600 too high" & vbLf)
			Case -400
				Console.Write("400 too high" & vbLf)
			Case -200
				Console.Write("200 too high" & vbLf)
			Case -100
				Console.Write("100 too high" & vbLf)
			Case Else
				Console.Write("too high" & vbLf)
			End Select
	End If

Which is brilliant because I didn't even consider Select Case as a method, I was trying to do some weird math equations. So I should be able to just plug this in and plug in my own variable names. Thank you so much!

1 Like

You're welcome! Glad to be of help.

If you could do me one favor and that's marked the thread as solved that'd be great.