Help Continue VBS String? - vbscript

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

Related

GoTo "Expected Statement"

I am attempting to use the GoTo command (I do not know any alternatives and it works fine in batch). Whenever I attempt to load the program, I get this error:
Here is basically where the error is (line 11 column 3)
top:
input = InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If input = "" Then
Y = MsgBox("You inputed nothing!", vbRetryCancel+64, "Huh?")
If Y = 2 Then
WScript.Quit
Else
If Y = 4 Then
GoTo top
Else
If input = 2 Then
WScript.Quit
VBScript doesn't have a Goto statement, and there's a cleaner approach anyway.
Do
input = InputBox(...)
If IsEmpty(input) Or input = "2" Then
WScript.Quit
ElseIf input = "" Then
MsgBox "No input."
End If
Loop Until input <> ""
try this
Option Explicit
Dim Input ' as string
Dim Y ' as msgbox response
Input = ""
Do Until Input <> ""
Input= InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If Input = "" Then
Y = Msgbox ("You input nothing", vbRetryCancel, "Huh?")
If Y = vbCancel Then
WScript.Quit
End If
ElseIf Input = "2" Then
WScript.Quit
End If
Loop
' Proceed here if input is valid
Vbscript is a structured programming language and one of the main goals of structured programming is to eliminate the goto statement as it's considered harmful. Vbscript does have a goto for exceptions, but these are only meant for resource cleanup prior to a program exit.

VBScript Guess a number

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()

trouble with the error expected "end"

what am I doing wrong in this script?
Randomize
value=int((150 * Rnd + 1) * Rnd + lowerbound)
guess+inputbox("guess the number","guess","guess here")
if guess=value then
msgbox("correct"),0+64+4096,("guess")
wscript.quit
else
if guess < value then
msgbox("too low"),0+32+4096,("guess")
once=1
end if
else
msgbox("too high"),0+32+4096,("guess")
end if
once=1
do
guess=inputbox("try again","guess","guess here")
if guess=value then
msgbox("correct"),0+64+4096,("guess")
else
if guess < value then
msgbox("too low"),0+32+4096,("guess")
end if
else
msgbox("too high"),0+32+4096,("guess")
end if
loop
If you can figure out what is going wrong that would be great.
It has been saying
expected "end"
and when I do what it says I will run it and it still won't work.
Have edited the code to properly indent it which should help highlight the issue. The error is because an If statement can only have one Else condition, the basic structure of an If statement is;
If <boolean condition> Then
<true outcome>
Else
<false outcome>
End If
At the moment you have multiple instances of
If <boolean condition> Then
<true outcome>
Else
<false outcome>
Else
<unknown condition>
End If
which is invalid syntax because a basic If statement can only return True or False.
However there is also ElseIf which allows multiple boolean conditions to be specified and return different outcomes.
Which looks like this;
If <boolean condition> Then
<true outcome>
ElseIf <boolean condition> Then
<true outcome>
ElseIf <boolean condition> Then
<true outcome>
Else
<false outcome>
End If
Based on how you are nesting the If statements the code in question could be re-written like this;
Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
guess = InputBox("guess the number", "guess", "guess here")
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
WScript.Quit
ElseIf guess < value Then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End if
once = 1
Do
guess = InputBox("try again", "guess", "guess here")
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
ElseIf guess < value then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End If
Loop
Couple of things to note about the example above
Replaced the + with = in the line
guess+inputbox("guess the number","guess","guess here")
as it won't assign the result of InputBox() to guess which is what I'm assuming you are doing. If you were trying to concatenate the result of InputBox() to guess that still wouldn't work you would have to use
guess = guess + InputBox("guess the number", "guess", "guess here")
If that was the case though personally I prefer to use & over + for string concatenation.
The brackets in MsgBox() appear to be a bit odd, usually you would call MsgBox() without brackets if not returning the result to a variable, like this;
MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
but if you did want to include the brackets (as I prefer to do) you can just use Call like this
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
which avoids the dreaded
Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub
but still allows the function to be surrounded with brackets when no value is being returned.
You may also noticed that I replaced the hardcoded numeric values in MsgBox() with the Named Constants which is easier for others to read and interpret their meaning. They are also hard-boiled into VBScript so there is no excuse for not using them.
Thinking about what you trying to do thought I could re-factor the code a little bit to hopefully make it work as you expected
Dim value, guess, msg, once
Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
Do
If once = 0 Then
msg = "guess the number"
Else
msg = "try again"
End If
guess = CInt(InputBox(msg, "guess", "guess here"))
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
WScript.Quit
ElseIf guess < value then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
End If
Loop
Things to take from this;
We only need the one InputBox() inside the loop to handle both the first initial guess and the subsequent "try again" attempts. This means we are not duplicating the same piece of code again, see DRY Principle.
InputBox() returns a string so at first the code was trying to compare a string value with a integer value which will give all sorts of weird results. By casting to integer using CInt() the comparison starts to work as expected.

In VBScript, I can not stop my loop from repeating

I have looked hard to resolve this issue, but I am coming up short.
Here is my code:
'attempts and completions loop
'attempts
do
do
Wscript.StdOut.WriteLine "How many attempts did " & QB & " throw: "
attempts = Wscript.StdIn.ReadLine
if IsNumeric(attempts) then
attempts = CInt(attempts)
else
Wscript.StdOut.Write "You did not enter a number. Please try again."
end if
loop while IsNumeric(attempts) = false
'completions
do
Wscript.StdOut.WriteLine "How many completed passes did " & QB & " throw for: "
completions = Wscript.StdIn.ReadLine
if IsNumeric(completions) then
attempts = CInt(completions)
else
Wscript.StdOut.Write "You did not enter a number. Please try again."
end if
loop while (completions) = false
if attempts > completions then
exit do
else
Wscript.StdOut.Writeline "Completions can not be more that attempts please try again."
end if
loop while attempts < completions
When I enter more completions than attempts, I get my desire result. But when I enter more attempts than completes it will still loop through this code. Can I please get some assistance.
Nevermind my comment...I see it now. In the loop for Completions, you are setting the attempts variable when you should be setting the completions variable.
if IsNumeric(completions) then
attempts = CInt(completions)
So when you're comparing, attempts is always blank or whatever you've initialized it to.
Standard copy/paste error :)

QTP: Checking If an array of strings contains a value

I am having trouble getting my test case to run correctly.
The problem is in the code below, the first if statement to be exact. QTP complains that an object is required
For j=Lbound(options) to Ubound(options)
If options(j).Contains(choice) Then
MsgBox("Found " & FindThisString & " at index " & _
options.IndexOf(choice))
Else
MsgBox "String not found!"
End If
Next
When I check the array I can see that it is populated correctly and 'j' is also the correct string.
Any help with this issue would be greatly appreciated.
Strings in VBScript are not objects, in that they do not have member functions. Searching for a substring should be done by using the InStr function.
For j=Lbound(options) to Ubound(options)
If InStr(options(j), choice) <> 0 Then
MsgBox("Found " & choice & " at index " & j
Else
MsgBox "String not found!"
End If
Next
A concise way to check if an array of strings contains a value would be to combine the Filter and UBound functions:
If Ubound(Filter(options, choice)) > -1 Then
MsgBox "Found"
Else
MsgBox "Not found!"
End If
Cons: you don't get the indexes where the elements are found
Pros: it's simple and you have the usual include and compare parameters to specify the matching criteria.
Function CompareStrings ( arrayItems , choice )
For i=Lbound(arrayItems) to Ubound(arrayItems)
' 0 - for binary comparison "Case sensitive
' 1 - for text compare not case sensitive
If StrComp(arrayItems(i), choice , 0) = 0 Then
MsgBox("Found " & choice & " at index " & i
Else
MsgBox "String not found!"
End If
Next
End Function
Hi if you check exact String not sub-String in the array use StrComb because if use InStr then if array = "apple1" , "apple2" , "apple3" , "apple" and choice = "apple" then all will return pass for every array item.
Function CompareStrings ( arrayItems , choice )
For i=Lbound(arrayItems) to Ubound(arrayItems)
' 1 - for binary comparison "Case sensitive
' 0 - not case sensitive
If StrComp(arrayItems(i), choice , 1) = 0 Then
CompareStrings = True
MsgBox("Found " & choice & " at index " & i
Else
CompareStrings = False
MsgBox "String not found!"
End If
Next
End Function

Resources