capa
capa

Reputation: 21

goto unexpected with blank choice

My problem: I set a VAR with prompt and three IF to let choice. Fourth choice doesn't work. I would like that if i push enter without type anything, it goes back to :MOSTRA. Actually leaving it blank and typing enter on the keyboard it give back GOTO WAS UNEXPECTED and close CMD. Where am i wrong?

:MOSTRA
ECHO Make your choice.
ECHO.
ECHO A) Ok
ECHO B) Ko
ECHO Esci
SET /P choose=Scelta: 
if /I %choose%==A GOTO OK
if /I %choose%==B GOTO KO
if /I %choose%==esci GOTO FINE
if /I %choose%=="" GOTO ERROR


:ERROR
ECHO You type nothing.
ECHO.
GOTO MOSTRA

:KO
ECHO Bad choice
ECHO.
GOTO MOSTRA

:OK
ECHO Right choice
ECHO.
GOTO MOSTRA

:FINE
exit

Upvotes: 1

Views: 703

Answers (1)

Magoo
Magoo

Reputation: 79982

SET /P choose=Scelta: 
if /I "%choose%"=="A" GOTO OK
if /I "%choose%"=="B" GOTO KO
if /I "%choose%"=="esci" GOTO FINE
if /I "%choose%"=="" GOTO ERROR

CMD will substitute the value of choose in the place of %choose% so

if /I %choose%==A GOTO OK 

is interpreted as

if /I ==A GOTO OK

which generates the error.

There is a more subtle error, too.

If you first choose a or b, you get the "right choice" or "bad choice" response, correctly BUT if your next choice is simply ENTER the response doesn't change - the previous response will be repeated.

The reason is that set /p does not change choose if you simply type ENTER. What you need is

SET "choose="
SET /P choose=Scelta: 
if /I "%choose%"=="A" GOTO OK

which sets choose to [nothing]

Upvotes: 3

Related Questions