Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Goto was unexpected at this time - batch

Tags:

goto

I am trying to make a batch text-based game. But i encountered a problem just starting to write it what I have never encountered before.

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
echo enter your choice:
set /p startchoice =
if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame

When i type in either 1 or 2, it shows the error "goto was unexpected at this time" How do I fix it?

like image 891
user1935683 Avatar asked Dec 11 '22 19:12

user1935683


1 Answers

Your startchoice isn't being set correctly. Use the alternate syntax for set /p where you supply the prompt there (and remove the space between startchoice and the assignment (=) operator - I think it's actually the cause of the problem, but you can reduce your batch file by a line if you use the set /p <variable>=<Prompt> syntax).

I added two targets for the goto, and echo statements so you could see they were reached:

:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
set /p startchoice=Enter your choice:

if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame
:changes
echo Changes
goto end
:startgame
echo StartGame
:end
like image 185
Ken White Avatar answered Jan 05 '23 02:01

Ken White