Solved missing operand; How i fix?

November 4, 2015 at 12:21:34
Specs: Windows 10
Hello, i've just started learning batch, or cmd (idk).
Anyway, when i test my "game" i get the error: missing operand spammed over my whole screen.
Anyone know what i got to change? (plz explain why i have to whatever i have to do too)

The code goes like this:

:attack1
cls
:'fight_loop_1'
set /a you=%4%
set /a him=%4%
set /a num=%random%
if %num% gtr 2 goto 'fight_loop_1'
if %num% lss 1 goto 'fight_loop_1'
if %num%==1 you-1 echo you got hit! goto 'fight_loop_1'
if %num%==2 him-1 echo you hit him! goto 'fight_loop_1'
if %you% lss 1 echo press any button pause>nul goto lose1
if %him% lss 1 echo press any button pause>nul goto lose2


See More: missing operand; How i fix?

Report •

#1
November 4, 2015 at 14:00:23
✔ Best Answer
You'd have to change a lot to make it work.
Some basic stuff about variables VS. literals:
%variable% is a variable.
variable is a literal.
set variable=5
Now, %variable% is still a variable, but is containing the number "5".

The missing operand errors are due to these commands:

set /a you=%4%

set /a him=%4%

and
set /a num=%random%

Set /a means to perform a mathematic operation, but when not all the required stuff is present, it gives you a 'Missing operand' error.

If you're trying to set the health, you have to use set you=4
%4% means that it's a variable named 4, since I doubt there is one, the field is blank. Also, using numbers as variable names is not recommended, as they tend to not work.

The reason you're getting so MANY errors is because you're checking if the %num% variable is greater than two or less than 1 and looping while it doesn't meet those conditions. The chance of %random% being 1 or 2 is extremely low.

Structure when creating scripts is important, here's a more structured (and fixed) version of your code, with explanations as to what the code is doing:

:attack
:: Set health for 'you' and 'him'
set you=4
set him=4
cls

:fight_loop_1
:: Set %num% to be a random number between 0 and 2
set num=%random% %% 3
:: If %num% is 0, set a new number
if %num%==0 (
	goto fight_loop_1
)
:: Check if 'you' is out of health
if %you%==0 (
	echo Press any key...
	pause > nul
	goto lose1
)
:: Check if 'him' is out of health
if %him%==0 (
	echo Press any key...
	pause > nul
	goto lose2
)
if %num%==1 (
        :: Subtract one from the health of 'you'
	set /a you=%you%-1
	echo You got hit!
	goto fight_loop_1
)
if %num%==2 (
        :: Subtract one from the health of 'him'
	set /a him=%him%-1
	echo You hit him!
	goto fight_loop_1
)
goto fight_loop_1

Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ


Report •
Related Solutions


Ask Question