1

i am new here so i'll try to be as good as i can.

So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.

Here is one of the parts i'd like to fix:

@echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do? 
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT

Thanks in advance

Marco
  • 13
  • 4
  • possible duplicate of [What is the proper way to test if variable is empty in a batch file, If NOT "%1" == "" GOTO SomeLabel, fails if %1 has quotes](http://stackoverflow.com/questions/2541767/what-is-the-proper-way-to-test-if-variable-is-empty-in-a-batch-file-if-not-1) – Jason Aller Dec 15 '14 at 17:46
  • Maybe `help` is a better word than `instructions` - shorter and more familiar. – Stephan Dec 15 '14 at 17:59

1 Answers1

4

it's not the set /pthat crashes, but:

if %input%==new 

if %input% is empty, this is parsed as:

if ==new 

obviously a syntax error. To avoid this, use:

if "%input%"=="new"

An empty input will then be parsed as:

if ""=="new"

which works fine.

The same applies when the variable contains only spaces and/or tabs:

if == new (syntax error) versus if " " == "new" (running fine)

Complete code like this:

:Menu
set input=
set /p input=What do you want to do? 
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • But the thing i'd like to do is that when ccidently put something that is not in the options aka "IF" that it doesn't crash, that it only turns back to :Menu and letting it put the info again. Thanks though – Marco Dec 15 '14 at 17:45
  • don't see the forest because of all those trees? See my edit. – Stephan Dec 15 '14 at 17:52