VBScript Guess a number - vbscript

So I am at a bit of a loss. I have been working on this script for a "Guess a Number" game for a class and so far I have been successful. Now the assignment requires that when the user guesses either too high or too low it should give them better clues as to how far away they are.
If, for example, the user is 50 numbers away it should notify them they are cold. If they are 30 numbers away they are warm, 10 numbers away they are hot...
I can't figure that part out.
Any help is greatly appreciated.
Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * 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

Your question is probably off topic for S.O. I will give you a hint anyway because I have been in your shoes once...
Does this ring a bell on?
coldThresHold = 20
If userNumber = intRandomNo + coldThresHold Then YouGuessed()
Else If userNumber > intRandomNo + coldThresHold Then YouAreCold()
Else If userNumber > intRandomNo Then YouAreHot()
Else If userNumber < intRandomNo - coldThresHold Then YouAreCold()
Else userNumber < intRandomNo Then YouAreHot()

Related

First VBScript not working as expected [duplicate]

I am at a complete loss here. I have been beating my head against a wall all weekend and I cannot for the life of me figure out how to make my program work. This is a college class assignment and it is my first programming assignment ever, so this is a real challenge for me. I have Googled until my fingers were bleeding (not really) and I have only come across marginally helpful information. Here is my assignment (I am deeply sorry if I get the formatting wrong):
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * 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
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than
'80,60,40,20,10 or farther from the correct guess, but still too low
If FormatNumber(intUserNumber) < intRandomNo Then
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than
'80,60,40,20,10 or closer to the correct guess, but still too high
If FormatNumber(intUserNumber) > intRandomNo Then
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
This is blowing my mind. The only thing that has been remotely helpful has been this link: VBScript Guess a number and with that being said, I have no earthly idea how to even implement that code into my own. It looks like it would work, but whenever I try it VBScript throws tons of "expected end of statement" errors. My own code is near worthless in the effort, but here it is:
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * 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 - 80 Then
MsgBox "Your guess was too low by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 60 Then
MsgBox "Your guess was too low by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 40 Then
MsgBox "Your guess was too low by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 20 Then
MsgBox "Your guess was too low by 20. Try again", ,cGreetingMsg
Elseif FormatNumber(intUserNumber) < intRandomNo - 10 Then
MsgBox "Your guess was too low by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo Then
MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
If FormatNumber(intUserNumber) > intRandomNo - 80 Then
MsgBox "Your guess was too high by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 60 Then
MsgBox "Your guess was too high by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 40 Then
MsgBox "Your guess was too high by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 20 Then
MsgBox "Your guess was too high by 20. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 10 Then
MsgBox "Your guess was too high by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo Then
MsgBox "Your guess was still too low. Try again, please", ,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
Any and all help is greatly appreciated with this. Thank you.
Your main problem here is that all your numbers are really strings. The FormatNumber function takes a number and returns a string so intRandomNo is a string. InputBox also returns a string meaning intUserNumber is also a string. It will not be possible to calculate the difference if you keep those two values as strings. The first thing you have to do is to convert them to integers. I will not write all the code for you but I will give you a hint. First declare this variable:
Dim Difference
Then assign it this value inside your loop:
Difference = CInt(intRandomNo) - CInt(intUserNumber)
... and use that variable in your nested if. It should be easy after that.
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses, stringToNumber
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * 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
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than
'80,60,40,20,10 or farther from the correct guess, but still too low
If FormatNumber(intUserNumber) < intRandomNo Then
MsgBox "The number you chose is too low", ,cGreetingMsg
strOkToEnd = "no"
End If
If FormatNumber(intRandomNo) - intUserNumber = 10 Then
MsgBox "Too low 10 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 20 Then
MsgBox "Too low 20 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 40 Then
MsgBox "Too low 40 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 60 Then
MsgBox "Too low 60 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 80 Then
MsgBox "Too low 80 off!", ,cGreetingMsg
strOkToEnd = "no"
End If
If FormatNumber(intUserNumber) > intRandomNo Then
MsgBox "The number you chose is too high!", ,cGreetingMsg
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than
'80,60,40,20,10 or closer to the correct guess, but still too high
If FormatNumber(intUserNumber) - intRandomNo = 10 Then
MsgBox "Too high 10 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 20 Then
MsgBox "Too high 20 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 40 Then
MsgBox "Too high 40 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 60 Then
MsgBox "Too high 60 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 80 Then
MsgBox "Too high 80 off!", ,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

VbScript make computer say something error

I have this file that makes a computer say something. I want it to loop with a VbCancel function. I get this error. Code so far:
Do
Dim Message, Speak
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Speak)
Speak.Speak Message
If Len(Speak) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Sub
End If
Loop
BTW the error is Invalid exit statement
I'm working on Windows 7
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop
You had several issues here
Exit Sub is for subroutines. You were trying to exit a Do loop
Speak is an object. I dont know if it has a string property but it is not itself a string. Both Len(Speak) and "You entered: " & Speak has Speak changed to Message.
You were missing an End If
I moved the Dim statements out of the loop. No point recreating the object over and over again.
Dim Message, Speak
Do
Message=InputBox("Enter text","Speak")
Set Speak=CreateObject("sapi.spvoice")
MsgBox ("You entered: " & Message)
Speak.Speak Message
If Len(Message) = 0 Then
MyMessageBox = MsgBox("Click Yes if you mean to Cancel." & vbCrLf & _
"If you mean to enter a zero length string, click No.", vbYesNo, "DO YOU MEAN TO CANCEL?")
If MyMessageBox = vbYes Then
MsgBox "Operation Cancelled"
Exit Do
End If
End If
Loop

I need some idea to correct this VBS code

Hello everyone.
Hope everyone is fine.
I need help with this code below with the section (comment line) saying "'Help needed from here'":
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem in colItems
MsgBox "Welcome to the ALARM tool",0+0,"ALARM tool"
MsgBox "Note: 1) If the computer is given a shutdown or a restart command then the alarm will be disabled. 2) This ALARM tool dosen't support decimals & words. If you will enter a decimal, the elapsed message box will appear without processing the alarm due to the error. If you enter words then an error message will appear.",0+0,"ALARM tool"
thour=InputBox ("Type which hour you want the alarm to elapse in 24-hour format from 0 to 23 inclusive (not 24; instead 0): (E.G: 19, 15)","ALARM tool")
If thour="" Then
MsgBox "The value is empty, so no alarm is set.",16,"ALARM tool"
WScript.Quit
End If
tmin=InputBox ("Type which minute you want the alarm to elapse from 0 to 60 inclusive:","ALARM tool")
If tmin="" Then
MsgBox "The value is empty, so no alarm is set.",16,"ALARM tool"
WScript.Quit
End If
c=InputBox ("Enter your comment; leave it blank if you don't want one:","ALARM tool")
If c="" Then
MsgBox "Time: ''"+thour+":"+tmin+"'' Comment: None",0,"ALARM tool: Preview"
Else
MsgBox "Time: ''"+thour+":"+tmin+"'' Comment: ''"+c+"''",0,"ALARM tool: Preview"
End If
co=MsgBox ("Are you sure?",36,"ALARM tool")
If co="7" Then
MsgBox "Alarm cancelled!!!",0,"ALARM tool"
WScript.Quit
End If
'Help needed from here'
Do
If thour=objItem.Hour Then
If tmin=objItem.Minute Then
If c="" Then
MsgBox "ALARM set to elapse on "+thour+":"+tmin+" has elapsed!!!",0+0,"ALARM set to elapse in "+t
WScript.Quit
Else
MsgBox c,0+0,"ALARM set to elapse in "+t
WScript.Quit
End If
End If
End If
'No more help needed after this'
If Err.Number <> 0 Then
MsgBox "There's an error while setting the alarm. Maybe you typed words or it's an internal error. Please try again. Sorry for the inconvenience.",0,"ALARM tool"
WScript.Quit
End If
Loop
Next
You can give me the answer while even use it for yourself.Topic: Alarm for specific timeI will say thanks for helping me and it will be a very good help.

Help Continue VBS String?

I'm VERY new to VBScript and general computer programing in general.
IF I were to do this:
a=inputbox("Password:")
if a="Drop Zero" then
msgbox"Loging On"
else
msgbox"Invalid"
end if
a=inputbox("Hello, what would you like to know:")
I can't continue with the if and else after the second input box. Any help would be highly appreciated for a nOOb such as myself.!!
I'm in trouble understanding your question.
Anyway, if you need to quit when password is wrong, you can try this:
a=inputbox("Password:")
if a="Drop Zero" then
msgbox"Logging On"
else
msgbox"Invalid"
WScript.Quit
end if
a=inputbox("Hello, what would you like to know:")
UPDATE:
Look at this code taken from here:
Function ValidInteger(sNumber,iMin,iMax)
If IsNumeric(sNumber) = 0 Then
If InStr(sNumber,".") Then
If CLng(sNumber)>= iMin And CLng(sNumber)<= iMax Then
ValidInteger=""
Else
ValidInteger = "You must enter a number between " & iMin & " and " & iMax
End If
Else
ValidInteger = "You must enter a whole number"
End If
Else
ValidInteger = "You must enter a number value"
End If
End Function

Change password expiration date in Active Directory using VBS

I'm trying to change the password expiration date for a user in Active Directory using VBScript. I have the code to obtain information about a user's password, but I can't find anything about how to change it. Any help would be greatly appreciated!
Here's my code:
Const SEC_IN_DAY = 86400
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
Set objOU = GetObject("LDAP://CN=[username],OU=Users,OU=New York,OU=NA,OU=[domain],DC=[domain],DC=firm")
intCurrentValue = objOU.Get("userAccountControl")
If intCurrentValue and ADS_UF_DONT_EXPIRE_PASSWD Then
wscript.echo "The password does not expire."
Else
dtmValue = objOU.PasswordLastChanged
Wscript.echo "The password was last changed on " & _
DateValue(dtmValue) & " at " & TimeValue(dtmValue) & VbCrLf & _
"The difference between when the password was last set" & VbCrLf & _
"and today is " & int(now - dtmValue) & " days"
intTimeInterval = int(now - dtmValue)
Set objDomainNT = GetObject("WinNT://ropesgray")
intMaxPwdAge = objDomainNT.Get("MaxPasswordAge")
If intMaxPwdAge < 0 Then
WScript.Echo "The Maximum Password Age is set to 0 in the " & _
"domain. Therefore, the password does not expire."
Else
intMaxPwdAge = (intMaxPwdAge/SEC_IN_DAY)
Wscript.echo "The maximum password age is " & intMaxPwdAge & " days"
If intTimeInterval >= intMaxPwdAge Then
Wscript.echo "The password has expired."
Else
Wscript.echo "The password will expire on " & _
DateValue(dtmValue + intMaxPwdAge) & " (" & _
int((dtmValue + intMaxPwdAge) - now) & " days from today" & ")."
End If
End If
End If
'strUserPrincipalName = objOU.Get("userPrincipalName")
'strSAMAccountName = objOU.Get("sAMAccountName")
'strMaxPWAge = objOU.Get("manager")
'WScript.Echo strUserPrincipalName
'WScript.Echo strSAMAccountName
'WScript.Echo strMaxPWAge
You can use the pwdLastSet attribute to change the password expiration, but perhaps not in the way you want. pwdLastSet is the number of 100-nanosecond intervals since 12:00 am January 1, 1601.
According to Microsoft documentation, this attribute accepts only two values 0 or -1.
try this :
Set pwdLastSet to 0, this means that the password has never been set.
Then, Set pwdLastSet to -1, this means that the password has just been set. So the value that appears in pwdLastSet is the current date/time.
I use to use in in W2K3 and it's still working on W2H8 R2.
You can find there a tool (sorry in french) that allow you to create date/time from number of 100-nanosecond intervals since 12:00 am January 1, 1601.
Be carefull It lengthens the password duration, which is not good for security.
I hope it helps.
JP

Resources