set /p empty answer crash

情到浓时终转凉″ 提交于 2019-12-17 14:22:48

问题


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


回答1:


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. 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


来源:https://stackoverflow.com/questions/27489804/set-p-empty-answer-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!