-1

I'm making a game and for set /a "var", var becomes 0 and missing operator too.

This is a bit of code

set /p confirm=y/n : 
if '%confirm%' == 'y' (
set /a partner=Ignite
set /a monsterlevel=5
set /a reqexp=100
set /a currentexp=0
set /a element=Fire
set /a evolution=Igneous
set /a move1=Scratch
set /a move2=Burn
set /a move3=Flame Spread
set /a move4=Quick Slash
set /a hp=100
goto mymonster
)
if '%confirm%' == 'n' goto pick

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markab1
  • 1
  • 2
  • 2
    why do you use `/a`? `set /a partner=Ignite` sets `%partner%` to the value of `%Ignite%`, which is empty. `set /a move3=Flame Spread` tries to set `%move3%` to `%flame% [missing operator here] %Spread%`. – Stephan Sep 05 '16 at 11:45
  • 1
    Isn't `/a` for arithmetic use? Meaning that leaving the `/a` out should solve the problem or not? `set /?` tells you that the argument has to be a numeric value which is not the case. – geisterfurz007 Sep 05 '16 at 12:00
  • What do you mean by "missing operator too"? Is that an error message? – Peter Mortensen Sep 12 '16 at 17:03

1 Answers1

0

Open a command prompt window, run set /? and read all help pages output into the console window.

The option /A is for arithmetic expressions and should be never used to assign a string value to an environment variable. Environment variables are always of type string.

On the line

set /a partner=Ignite

the Windows command processor interprets Ignite as name of an environment variable which string value converted to an integer value should be assigned to variable partner converted back from integer to string. As there is most likely no variable Ignite the value 0 is assigned to partner as string.

On the line

set /a move3=Flame Spread

the Windows command processor interprets Flame and Spread as name of environment variables. But there is no operator between and therefore the arithmetic expression is invalid because of the missing operator.

The solution:

@echo off
setlocal EnableDelayedExpansion
set "confirm=n"
set /P "confirm=y/n : "
if /I "!confirm!" == "y" (
    set "partner=Ignite"
    set "monsterlevel=5"
    set "reqexp=100"
    set "currentexp=0"
    set "element=Fire"
    set "evolution=Igneous"
    set "move1=Scratch"
    set "move2=Burn"
    set "move3=Flame Spread"
    set "move4=Quick Slash"
    set "hp=100"
    goto mymonster
)
if /I "!confirm!" == "n" goto pick
rem The user has entered whether y/Y nor n/N. What to do now?

The environment variable confirm is defined with a default value in case of batch user just hits RETURN or ENTER without entering anything at all. In this case the environment variable confirm keeps its current value or is still undefined if not defined before.

The string comparison is done case-insensitive because of IF option /I. Run in a command prompt window if /? for help on this command .

And the string comparison is done with using delayed expansion. This is for security as the batch user could enter by mistake or intentionally for example "< and making the string comparison now without using delayed expansion the batch processing would immediately exit on first IF condition line because of a syntax error.

Always use double quotes and not single quotes on comparing strings. Double quotes have a special meaning for Windows command processor on evaluating an IF condition, but single quotes don't. The character ' is interpreted on a string comparison with == like a letter or a digit, it has no special meaning. But please note that the Windows command processor compares the strings left and right of == with including the double quotes. So not the strings inside the double quotes are compared, but the strings with the double quotes which means if /I "!confirm!" == y would be never true.

See also the answer on How to set environment variables with spaces?

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143