Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

goto was unexpected at this time

@echo off
color 0a
title Horror Game
echo.
echo.
echo.
echo.
echo Welcome to the game
echo If you get scared
echo Feel free to leave
echo.
echo.
echo You are in a dark room.
echo It is cold.
echo All you hear is a scratching sound
echo near your feet.
echo What do you do?
echo.
echo.
echo 1.) Feel around you
echo 2.) Listen for anything else
set/p input = Command?
if %input% == "1" goto Feel
if %input% == "2" goto Listen
echo.
echo.
:Feel
echo You feel around and hear a growl.
echo As you realize the scratching was
echo on your leg.
echo. 
echo You remember nothing else.
pause
end

I am trying to make a text based game for cmd and whenever i try to enter a response is instantly closes and i can barely read out "goto was unexpected at this time"

like image 725
SammytheNerd Avatar asked Feb 14 '23 17:02

SammytheNerd


1 Answers

You did not form your tests correctly. You need to enclose the variable in double-quotes:

if "%input%" == "1" goto Feel
if "%input%" == "2" goto Listen

Note that you need an extra condition after these to deal with the possibility that they didn't enter 1 or 2.

You should consider using the choice command instead of set /p. Type choice /? from the command prompt.

In future, run your script from the command prompt if you're having trouble with premature termination. That way the window won't close on you and you'll have plenty of time to read the error.

You can run a command prompt quickly by pressing Windows Key-R, typing cmd and pressing Enter

like image 139
paddy Avatar answered Mar 30 '23 01:03

paddy