-1
:teattack1
set /a health-=!monsterdmg! # It said missing operator here
pause
set /a monsterhealth-=!playerdmg! # And Here
pause
if "!monsterhealth!" == lss 0 goto tewin
pause
goto testencounter
pause
goto encountermenu

I keep getting Missing Operator

I serched how to fix it but did not find anything

  • 2
    Did you tried to output your variables before? `echo health=!health! monster=!monsterdmg!` – jeb Jul 08 '16 at 09:12
  • 1
    What is `== lss` supposed to be doing? You only need one or the other. Also, you don't need quotes on the left side if you're going to use `lss`. – SomethingDark Jul 08 '16 at 09:49
  • 1
    It's also a good idea to enable `ECHO ON` just before the problem occurs and show the output in your question – jeb Jul 08 '16 at 10:20

2 Answers2

1

You need to add:

Setlocal EnableDelayedExpansion

to your script to allow the use of ! Check here for an overview of what this does and why its needed

Community
  • 1
  • 1
Revive
  • 2,248
  • 1
  • 16
  • 23
  • I hope that he has already enabled delayed expansion, at least. But you could be right! – jeb Jul 08 '16 at 09:17
  • @MineCakePvP - Well then `!monsterdmg!` hasn't been set correctly. – SomethingDark Jul 08 '16 at 09:50
  • 1
    That would give a missing operand error, not missing operator. @MineCakePvP do you have the # comments in your code? Make sure no comments are on the same line as these set operations. – Revive Jul 08 '16 at 10:07
0

Open a command prompt window, enter set /? and read all output help pages carefully.

It is explained that when using set for an arithmetic expression, i.e. set /A, then variable names can be specified directly without expansion. Windows command processor interprets automatically each string not being a number or an operator as name of a variable and access the current variable value on evaluating the arithmetic expression.

Next run in command prompt window if /? and read again all output help pages carefully.

The line

if "!monsterhealth!" == lss 0 goto tewin

is definitely invalid as it contains the operator == and the operator lss which of course can't work.

This improved code is with all coding mistakes fixed.

:teattack1
set /a health-=monsterdmg
pause
set /a monsterhealth-=playerdmg
pause
if %monsterhealth% LEQ 0 goto tewin
pause
goto testencounter
pause
goto encountermenu

However, the environment variables health, monsterdmg, monsterhealth and playerdmg should all exist and have an integer value assigned as otherwise Windows command interpreter uses value 0 for each variable making the code not really useful.

Mofi
  • 46,139
  • 17
  • 80
  • 143