Reputation: 5
Hello everyone i put together this neat little script that quiz's the knowledge of the user on there Star Trek knowledge the script executes perfectly, but when hitting either OK, Cancel or X the questions are still answered as correct. I do know i need a vbCancel set in there somewhere but not sure about how to add it to the script.
'***********************************************************************
'Script Name: StarTrekQuiz.vbs
'Author: CharlesP
'Created: 03/31/15
'Description: This script creates a Star Trek Quiz game.
'***********************************************************************
'Perform script initialization activities
Option Explicit
Dim intPlayGame, strSplashImage, intNumberCorrect, strFederationRank
Dim objFsoObject, objQuestionDict, strAnswer, strQuestion
Const cTitlebarMsg = "The Star Trek Quiz Game"
Set objQuestionDict = CreateObject("Scripting.Dictionary")
objQuestionDict.Add "What was the Science Officer's name in the original Star Trek series?", "spock"
objQuestionDict.Add "What Star Trek villain appeared in both the original series and a Star Trek movie?", "khan"
objQuestionDict.Add "What was the numeric designation of Voyager's on-board Borg?", "7"
objQuestionDict.Add "Name the only Star Trek character to regularly appear on two series and at least two Star Trek movies?",
"worf"
objQuestionDict.Add "What is the last name of your favorite Captain?", "kirk picard sisco janeway archer"
objQuestionDict.Add "Who directed the first Star Trek movie?", "Robert Wise"
objQuestionDict.Add "In what Star Trek TOS episode did the tribbles FIRST appear?", "The Trouble With Tribbles"
objQuestionDict.Add "In what episode of Star Trek TNG did the Enterprise transport samples of Plasma Plague for medical
research?", "The Child"
objQuestionDict.Add "Who voices the computer in TNG?", "Majel Barrett"
objQuestionDict.Add "In TOS ~The Ultimate Computer~ who was the captain of the U.S.S. Excalibur?", "Captain Harris"
'Start the user's score at zero
intNumberCorrect = 0
'Display the splash screen and ask the user if he or she wants to play
strSplashImage = space(11) & "********" & vbCrLf & _
" ******************" & space(20) & "**************************" & _
space(15) & vbCrLf & "*" & space(30) & "*" & space(18) & _
"**" & space(39) & "*" & vbCrLf & " ******************" & _
space(20) & "*************************" & vbCrLf & space(31) & _
"******" & space(26) & "***" & vbCrLf & _
space(34) & "******" & space(22) & "***" & vbCrLf & _
space(37) & "******" & space(17) & "***" & vbCrLf & _
space(26) & " ****************************" & vbCrLf & _
space(26) & "*******************************" & vbCrLf & _
space(26) & "******************************" & vbCrLf & _
space(26) & " ****************************" & vbCrLf & vbCrLf & vbCrLf & _
"Would you like to boldly go where no one has gone before?"
'Ask the user to play
intPlayGame = MsgBox(strSplashImage, 36, cTitlebarMsg)
If intPlayGame = vbYes Then 'User elected to play the game
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If Trim(strAnswer) <> "" Then
If Instr(strAnswer, objQuestionDict(strQuestion)) Or Instr(objQuestionDict(strQuestion), strAnswer) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox "Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
End If
Next
Select Case intNumberCorrect
'User got 10 of 10 answers right
Case 10 : strFederationRank = "Fleet Admiral"
'User got 9 of 10 answers right
Case 9 : strFederationRank = "Admiral"
'User got 8 of 9 answers right
Case 8 : strFederationRank = "Vice Admiral"
'User got 7 of 8 answers right
Case 7 : strFederationRank = "Rear Admiral-Upper Hall"
'User got 6 of 7 answers right
Case 6 : strFederationRank = "Rear Admiral-Lower Hall"
'User got 5 of 5 answers right
Case 5 : strFederationRank = "Captain"
'User got 4 of 5 answers right
Case 4 : strFederationRank = "Commander"
'User got 3 of 5 answers right
Case 3 : strFederationRank = "Lieutenant-Commander"
'User got 2 of 5 answers right
Case 2 : strFederationRank = "Lieutenant"
'User got 1 of 5 answers right
Case 1 : strFederationRank = "Lieutenant Junior Grade"
'User did not get any answers right
Case 0 : strFederationRank = "Ensign"
End Select
MsgBox "You answered " & intNumberCorrect & " out of 10 correct." & _
vbCrLf & vbCrLf & "Your Star Fleet rank is : " & _
strFederationRank, , cTitlebarMsg
Else 'User doesn't want to play
MsgBox "Thank you for taking the Star Trek Quiz" & _
vbCrLf & vbCrLf & "Live long and prosper!", , cTitlebarMsg
WScript.Quit()
End If
Any help would be much appreciated thank you all in advance.
Upvotes: 0
Views: 2306
Reputation: 30103
Unlike MsgBox
function or Popup
method, InputBox
function has no vbCancel
option.
If the user clicks OK or presses ENTER, the
InputBox
function returns whatever is in the text box. If the user clicks Cancel or ×, the function returns a zero-length string (""
).
Strictly speaking, above MSDN quotation is not right. If the user
clicks Cancel or × button or presses Esc key, the function returns Empty
value (the same as if a variable is uninitialized.
Next code snippet could help.
' Ask the user to play
intPlayGame = MsgBox(strSplashImage, vbYesNo + vbQuestion, cTitlebarMsg)
Dim WshShell 'prepare for POPUP method
Set WshShell = WScript.CreateObject("WScript.Shell")
If intPlayGame = vbYes Then 'User elected to play the game
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If VarType( strAnswer)=0 Then
intPlayGame = WshShell.Popup( _
"Cancel pressed or window closed. Continue?", 7, _
cTitlebarMsg, vbYesNo + vbQuestion)
If not intPlayGame = vbYes Then Wscript.Quit
strAnswer=""
End If
if strAnswer="" Then strAnswer="???"
If Instr(1, strAnswer, objQuestionDict(strQuestion), vbTextCompare) _
Or Instr(1, objQuestionDict(strQuestion), strAnswer, vbTextCompare) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox strAnswer & " Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
Next
Select Case intNumberCorrect
Predefined MsgBox / Popup Constants
' Button Type
' vbOKOnly 0 Display OK button only.
' vbOKCancel 1 Display OK and Cancel buttons.
' vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
' vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
' vbYesNo 4 Display Yes and No buttons.
' vbRetryCancel 5 Display Retry and Cancel buttons.
' Icon Type
' vbCritical 16 Display Critical Message icon.
' vbQuestion 32 Display Warning Query icon.
' vbExclamation 48 Display Warning Message icon.
' vbInformation 64 Display Information Message icon.
' Default Button: MsgBox Function only
' vbDefaultButton1 0 First button is the default.
' vbDefaultButton2 256 Second button is the default.
' vbDefaultButton3 512 Third button is the default.
' vbDefaultButton4 768 Fourth button is the default.
' Modality of the box: MsgBox Function only
' vbApplicationModal 0 Application modal. The user must respond to the message box before continuing work in the current application.
' vbSystemModal 4096 System modal. On Win16 systems, all applications are suspended until the user responds to the message box. On Win32 systems, this constant provides an application modal message box that always remains on top of any other programs you may have running.
' Return Value
' vbOK 1 OK
' vbCancel 2 Cancel
' vbAbort 3 Abort
' vbRetry 4 Retry
' vbIgnore 5 Ignore
' vbYes 6 Yes
' vbNo 7 No
' vbTrue -1 nSecondsToWait elapsed, the user did not click a button before
'
Edit:
@Ekkehard.Horner is right as CStr(VarType(strAnswer)) & TypeName(strAnswer)
returns 0Empty
if clicked Cancel or × in response to an InputBox
. My resource: MSDN VBScript language reference.
But it's a faultfinder's quibble only here because by the same MSDN: how expressions are compared: if one expression is Empty
and the other is a string, then perform a string comparison, using a zero-length string (""
) as the Empty
expression.
Thus, If strAnswer = "" ...
comparison gives the same result regardless of strAnswer
variable
""
, or Empty
.However, we could test VarType(strAnswer)
and allow a user to correctly terminate the quiz uncomplete (see update in code snippet, used Popup
method).
Upvotes: 1
Reputation: 15370
Clicking on cancel returns empty string in Inputbox.
So, check if the answer is "".
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If Trim(strAnswer) <> "" Then
If Instr(strAnswer, objQuestionDict(strQuestion)) Or Instr(objQuestionDict(strQuestion), strAnswer) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox "Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
End If
Next
Upvotes: 0