Leonardo Rosini
Leonardo Rosini

Reputation: 9

How to fix a goto problem in a batch file

I started learning programming a few days ago, and here i am with my first problems. I'm learning something about batch, but there is something in my code that is wrong, can you help me with this?

Specifically, the "goto" command it's not a go to. i've tried to put it in a single line, but it's like the goto doesn't exist. how can i fix it?

@echo off
:INIZIO
set /p "risp=Scegli un pokemon tra: Pikachu, Charizard e Blastoid:"
if [%risp%]=="pikachu" goto UNO
if [%risp%]=="charizard" goto UNO
if [%risp%]=="blastoid" goto UNO
:UNO
echo Bella scelta!
echo Vuoi attaccare o fuggire?
set/P risp= 
if [%risp%]=="attaccare" goto battaglia 1 
if [%risp%]=="fuggire" goto perdita
set/P risp=
:battaglia 1
echo e allora combattiamo!
echo Vuoi fare un attacco leggero o pesante?
set/P risp=
if [%risp%]==[leggero] echo il nemico sopravvive! ti attacca e il tuo pokemon muore! 
goto morte
if [%risp%]==[pesante] echo È superefficace, il pokèmon muore qui davanti!
goto win
:morte
set /p "risp=Continuare?
if [%risp%]=="si" echo e allora andiamo!
goto replay
if [%risp%]=="no" echo hai fallito la tua missione!
goto lose
:replay
set /p "risp=Scegli un pokemon tra:onix e reshiram:"
if [%risp%]=="onix" goto DUE 
if [%risp%]=="reshiram" goto DUE
:DUE
echo Vuoi attaccare o fuggire?
set /p "risp=
if [%risp%]==[attaccare] echo e allora combattiamo!
goto battaglia 2
if [%risp%]==[fuggire] echo sei riuscito a fuggire correndo all'impazzata
:battaglia 2
echo Vuoi fare un attacco leggero o pesante?
set /p "risp=
if [%risp%]==[pesante] echo È superefficace, il pokèmon muore qui davanti!
goto win
if [%risp%]==[leggero] echo il nemico non muore, ma la sua mossa non ti sfiora neanche
goto battaglia 2
:win
echo congratulazioni, hai vinto la tua prima battaglia!
pause
:perdita
echo sei scappato, ma non è ancora finita per te. preparati ad altri combattimenti in futuro!

I hope you can help me with this goto,but i need to learn much more about programming. thanks for the help!

Upvotes: 0

Views: 81

Answers (1)

MC ND
MC ND

Reputation: 70923

Without more information I think your problem are not goto commands but if conditionals

Suppose the user types pikachu on the first prompt. When the first if is reached the batch's parser will replace the variable read operation %risp% with the value inside the variable. So, your line

if [%risp%]=="pikachu" goto UNO

is executed as

if [pikachu]=="pikachu" goto UNO

You can see that [pikachu] is not equal to "pikachu" so the goto is not executed.

Some of your conditions are written as

if [%risp%]==[leggero]

In those cases the condition will work as the line will be parsed into

if [leggero]==[leggero]

but if the value retrieved from the user contains spaces the command will fail. This also needs to be changed.

Try with

if "%risp%"=="pikachu" goto UNO

That is, quoted values on both sides of the condition (so both sides can be equal), with the quotes protecting the presence of spaces.

This (or similar) change should be made in all your if commands.

Another problem with your gotos is how you name the labels where to jump. You can not include spaces in the label name. battaglia 1 and battaglia 2 are seen as battaglia and the goto command will jump to the nearest (from goto line to end of file, then from start of file) matching label.

Upvotes: 1

Related Questions