dustinrichards
dustinrichards

Reputation: 35

"Expected End of Statement" in Loop

I am getting an "expected end of statement" error at line 26, the third to last line. If you look at the code, it is a simple game that has one person enter a word, the script replaces all consonants with underscores, and the second player has to guess the word. Line 26 is, I think, the only thing wrong with this program.

phrase=inputbox("Player 1: Enter a phrase","Guessing Game")
phrase=answer
phrase=Replace(phrase,"A","_")
phrase=Replace(phrase,"B","_")
phrase=Replace(phrase,"C","_")
phrase=Replace(phrase,"D","_")
phrase=Replace(phrase,"F","_")
phrase=Replace(phrase,"G","_")
phrase=Replace(phrase,"H","_")
phrase=Replace(phrase,"J","_")
phrase=Replace(phrase,"K","_")
phrase=Replace(phrase,"L","_")
phrase=Replace(phrase,"M","_")
phrase=Replace(phrase,"N","_")
phrase=Replace(phrase,"P","_")
phrase=Replace(phrase,"Q","_")
phrase=Replace(phrase,"R","_")
phrase=Replace(phrase,"S","_")
phrase=Replace(phrase,"T","_")
phrase=Replace(phrase,"V","_")
phrase=Replace(phrase,"W","_")
phrase=Replace(phrase,"X","_")
phrase=Replace(phrase,"Y","_")
phrase=Replace(phrase,"Z","_")
Do
result=InputBox "Player 2 enter your guess for" & phrase , "Guessing Game"
Loop until result==answer
msgbox "You got it!",vbokonly,"Guessing Game"

Upvotes: 0

Views: 3258

Answers (4)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

You're getting that error, because you used a function in an assignment without putting its parameter list in parentheses. Change this line:

result=InputBox "Player 2 enter your guess for" & phrase , "Guessing Game"

into this:

result=InputBox("Player 2 enter your guess for" & phrase , "Guessing Game")

This is one of VBScript's gotchas: depending on where/how you call a procedure or function, you must or mustn't put the parameter list in parentheses.

>>> String 3, "*"         'must not use parentheses here
>>> String(3, "*")
Cannot use parentheses when calling a Sub (0x414)

>>> Call String(3, "*")   'must use parentheses here
>>> Call String 3, "*"
Expected end of statement (0x401)

>>> v = String(3, "*")    'must use parentheses here either
>>> v = String 3, "*"
Expected end of statement (0x401)

To make matters worse, there are situations where parentheses can be used anyway, because they have a different meaning in that context:

>>> Hour(Now)

This actually works, because here the parentheses do not mean "parameter list", but "pass this argument by value". Take a look this article about the many interesting situations parentheses can create in VBScript.

The other mistake in your script, as Ekkehard.Horner already pointed out, is that you use == instead of = for comparing values.

As a side note: you seem to assume that the input will always consist of uppercase letters only, but you never enforce that anywhere. You may want to UCase your input or add a check to validate the input.

Upvotes: 1

Bond
Bond

Reputation: 16321

Seeing jmvp do such a good job optimizing your program made me want to do the same! :)

Here's my version. I added a constant to specify the application name (since it's used a few times) and also allow player 2 to quit playing by clicking Cancel (or entering nothing).

Const APP_TITLE = "Guessing Game"
strPhrase = InputBox("Player 1: Enter a phrase", APP_TITLE)

With New RegExp
    .Pattern = "[^AEIOU]"
    .Global = True
    strDisplay = .Replace(strPhrase, "_")
End With

Do
    strResult = InputBox("Player 2: Enter your guess for " & strDisplay, APP_TITLE)
Loop Until StrComp(strPhrase, strResult, vbBinaryCompare) = 0 Or Len(strResult) = 0

If Len(strResult) > 0 Then MsgBox "You got it!", vbOkOnly, APP_TITLE

Upvotes: 0

jmvp
jmvp

Reputation: 1

    Dim phrase As String = InputBox("Player 1: Enter a phrase", "Guessing Game")
    Dim charCount As Integer = phrase.Length
    Dim strMask As String = vbEmpty

    For x = 1 To charCount
        strMask &= "_"
    Next

    Dim guess As String = vbEmpty
    Do
        guess = InputBox("Player 2 enter your guess for " & strMask, "Guessing Game")
    Loop Until phrase = guess

    MsgBox("You got it!", vbOKOnly, "Guessing Game")

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

In VBScript, the 'equal' comparison operator is =. So change

Loop until result==answer

==>

Loop until result = answer

Upvotes: 2

Related Questions