1

Why does WSH throw an error on my code saying that there is an

"expected statement" in line 1 character 1.

Here is my code:

type = inputbox("What would you like to calculate with? (m = muliplication, d = division, a = addition, s = subtraction)")
if type = not "m" or "s" or "a" or "d" or "M" or "S" or "A" or "D" then
a=msgbox("You did not select one of the options.")
elseif type = "m" or "M" then
input1 = inputbox("You chose multiplication. What is the first number of your math sentence?")
input2 = inputbox("What is the other number of your math sentence?")
output = input1 * input2
display = msgbox("The answer to " & input1 & " " & chrw(0215) & " " & input2 & " is " & output & ".")
elseif type = "d" or "D" then
input1 = inputbox("You chose division. What is the dividend of your math sentence?")
input2 = inputbox("What is the divisor of your math sentence?")
output = input1 \ input2
display = msgbox("The answer to " & input1 & " " & chrw(247) & " " & input2 & " is " & output & ".")
elseif type = "a" or "A" then
input1 = inputbox("You chose addition. What is the first number of your math sentence?")
input2 = inputbox("What is the other number of your math sentence?")
output = input1 + input2
display = msgbox("The answer to " & input1 & " + " & input2 & " is " & output & ".")
elseif type = "s" or "S" then
input1 = inputbox("You chose subtraction. What is the first number of your math sentence?")
input2 = inputbox("What is the other number of your math sentence?")
output = input1 - input2
display = msgbox("The answer to " & input1 & " - " & input2 & " is " & output & ".")
end if

I know this is kind of obvious but I don't see any error on line 1 char 1.

Please explain to me how to fix this.

user692942
  • 16,398
  • 7
  • 76
  • 175

4 Answers4

1

You can't use the word "type" for a variable name in VBScript.

Tip: Use an editor that provides syntax highlighting, such as Notepad3, Notepad++, UltraEdit, VS Code, VBSEdit, etc. Issues like "type" being a reserved word is then instantly obvious:

enter image description here

LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • Although not used in VBScript it is used by Visual Basic so that makes it a reserved word just like `As` is also a reserved word even though it’s not used in VBScript. Good answer. – user692942 Nov 14 '21 at 08:49
  • Yeah that kind of works but when I run it the start inputbox works, but after that comes and error saying: Line 2 char 1 type mismatch "m". Btw I renamed all the variables in the if statements before testing. – Khizar Caliphate Nov 16 '21 at 07:59
  • @KhizarCaliphate The condition logic is wrong, you can't `= not`, instead try `If Not (variable = "m" Or variable = "s" Or ...)`. – user692942 Nov 16 '21 at 09:26
0

The reserved word is certainly an issue in the script as pointed out by this answer, but you also have some incorrect conditional logic.

It's not valid syntax to use Not = or trying to use short-circuited logic like Or "m" Or "s".

Instead, restructure the If statement like;

If Not (variable = "m" Or variable = "s" Or ...) Then '... and so on using variable = "value"

You can also LCase() your result from the InputBox() function to avoid needing to check both upper-case and lower-case variants, like this;

Dim choice: choice = LCase(InputBox("What would you like to calculate with? (m = muliplication, d = division, a = addition, s = subtraction)") & "") 'Returned value will be lower-case.

Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
0

@user692942 already gave the answer, the line

if type = not "m" or "s" or "a" or "d" or "M" or "S" or "A" or "D" then

is incorrect syntax.

As an alternative to multiple If/ElseIf statements, you can use Select Case/End Select:

Select Case LCase(Type)
    Case "a"
        input1 = inputbox("You chose addition. What is the first number of your math sentence?")
        input2 = inputbox("What is the other number of your math sentence?")
        output = input1 + input2
    Case "d"
        input1 = inputbox("You chose division. What is the dividend of your math sentence?")
        input2 = inputbox("What is the divisor of your math sentence?")
        output = input1 \ input2
        display = msgbox("The answer to " & input1 & " " & chrw(247) & " " & input2 & " is " & output & ".")
    Case "m"
        input1 = inputbox("You chose multiplication. What is the first number of your math sentence?")
        input2 = inputbox("What is the other number of your math sentence?")
        output = input1 * input2
        display = msgbox("The answer to " & input1 & " " & chrw(0215) & " " & input2 & " is " & output & ".")
    Case "s"
        input1 = inputbox("You chose subtraction. What is the first number of your math sentence?")
        input2 = inputbox("What is the other number of your math sentence?")
        output = input1 - input2
        display = msgbox("The answer to " & input1 & " - " & input2 & " is " & output & ".")
    Case Else
        a=msgbox("You did not select one of the options.")
End Select
Hel O'Ween
  • 1,423
  • 9
  • 15
-1

Start over. You cannot use "inputbox" because it runs server-side and not client-side. Even after resolving the other issues, the script will not work. Instead of using inputbox, create a form.

Try something like this:

<%
If Trim(Request.Form("strType")) = "" Then
    Response.Write  "<form action=""math.asp"" method=""post"">" & vbNewLine & _
        "   <table align=""center"" border=""1"">" & vbNewLine & _
        "       <tr>" & vbNewLine & _
        "           <td>" & vbNewLine & _
        "               <font face=""consolas"" size=""3em;"" color=""black"">What would you like to calculate with?  (m = muliplication, d = division, a = addition, s = subtraction)</font>" & vbNewLine & _
        "               <input style=""background-color:lightblue; color:navy; text-align:center; font-weight:bold;"" type=""text"" name=""strType"" value="""" size=""1"" />" & vbNewLine & _
        "           </td>" & vbNewLine & _
        "       </tr>" & vbNewLine & _
        "   </table>" & vbNewLine & _
        "</form>" & vbNewLine
    Response.End
End If
strType = LCase(Trim(Request.Form("strType")))
If strType <> "m" And strType <> "s" And strType <> "a" And strType <> "d" Then
    Response.Write  "You did not select one of the options."
    Response.End
End If
strP1 = "<input type=""hidden"" name=""strType"" value=""" & strType & """ />What is the 1st number of your math sentence?  "
strP2 = "What is the 2nd number of your math sentence?  "
strQ1 = "<input style=""background-color:lightblue; color:navy; text-align:center; font-weight:bold;"" type=""text"" name=""input1"" value="""" /><br />"
strQ2 = "<input style=""background-color:lightblue; color:navy; text-align:center; font-weight:bold;"" type=""text"" name=""input2"" value="""" /><br /></td></tr></table><center><input type=""submit"" value=""Calculate"" /></center></form>"
strE = strP1 & strQ1 & strP2 & strQ2
Response.Write  "<form action=""math.asp"" method=""post"">" & vbNewLine & _
    "   <table align=""center"" border=""1"">" & vbNewLine & _
    "       <tr>" & vbNewLine & _
    "           <td>" & vbNewLine & _
    "               <font face=""consolas"" size=""3em;"" color=""black"">" & vbNewLine
Select Case(strType)
    Case "m"
        If Trim(Request.Form("input1")) = "" Then
            Response.Write  "You chose multiplication.<br />" & strE
            Response.End
        Else
            If IsNumeric(Trim(Request.Form("input1"))) And IsNumeric(Trim(Request.Form("input2"))) Then
                Response.Write  "Answer:  " & Request.Form("input1") & " * " & Request.Form("input2") & " = " & Request.Form("input1") * Request.Form("input2")
            Else
                Response.Write  "Both values must be numeric."
                Response.End
            End If
        End If
    Case "d"
        If Trim(Request.Form("input1")) = "" Then
            Response.Write  "You chose division.<br />" & strE
            Response.End
        Else
            If IsNumeric(Trim(Request.Form("input1"))) And IsNumeric(Trim(Request.Form("input2"))) Then
                Response.Write  "Answer:  " & Request.Form("input1") & " / " & Request.Form("input2") & " = " & Request.Form("input1") / Request.Form("input2")
            Else
                Response.Write  "Both values must be numeric."
                Response.End
            End If
        End If
    Case "a"
        If Trim(Request.Form("input1")) = "" Then
            Response.Write  "You chose addition.<br />" & strE
            Response.End
        Else
            If IsNumeric(Trim(Request.Form("input1"))) And IsNumeric(Trim(Request.Form("input2"))) Then
                Response.Write  "Answer:  " & Request.Form("input1") & " + " & Request.Form("input2") & " = " & Request.Form("input1") + Request.Form("input2")
            Else
                Response.Write  "Both values must be numeric."
                Response.End
            End If
        End If
    Case Else
        If Trim(Request.Form("input1")) = "" Then
            Response.Write  "You chose subtraction.<br />" & strE
            Response.End
        Else
            If IsNumeric(Trim(Request.Form("input1"))) And IsNumeric(Trim(Request.Form("input2"))) Then
                Response.Write  "Answer:  " & Request.Form("input1") & " - " & Request.Form("input2") & " = " & Request.Form("input1") - Request.Form("input2")
            Else
                Response.Write  "Both values must be numeric."
                Response.End
            End If
        End If
End Select
%>