set /p empty answer crash - batch-file

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

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

Related

How to use goto command in if choice?

I am running the following program ,
#echo off
color 0a
cls
:MENU
echo What do you want to do ?
echo.
echo 1- Flash
echo 2- Wipe
echo 3- Check
echo 4- Go Back
echo.
set /p choice=
if %choice%==1 goto 1
if %choice%==2 goto 2
if %choice%==3 goto 3
if %choice%==4 goto 4
echo Invalid Choice
goto MENU
:1
flash.bat
:2
wipe.bat
:3
check.bat
:4
back.bat
In this program when i press any other key other than 1,2,3,4 it need to show invalid choice. But, its not working.. its shows error "goto command was unexpected at this time".
Please help me guys..When pressing anyother key it need to show invalid choice. Pls help guys...
I suggest, using choice, which does errorhandling on it's own, so you don't have to deal with that. There are many ways (many possible inputs) to break your script when using set /p.
If you want to keep with set /p, at least preset the variable (set /p doesn't set the variable to "empty" when you press ENTER, but lets it unchanged):
set choice=4
set /p choice=
echo %choice%
...
Additional bonus: you have a default action.

Batch. Trying to make text adventure, if else not working

i am trying to make a text adventure, but i'm kind of clueless how to get an if/else statement to work with a variable. probably a few mistakes in my code:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if %escape% = 1
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
else
goto dungeon
:dungeon
cls
echo well done.
pause
exit
If your if statement has has an accompanying else statement, you need to use parentheses. The else must also be in the format ) else (, like this:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if "%escape%"=="1" (
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
) else (
goto dungeon
)
exit /b
:dungeon
cls
echo well done.
pause
exit
I've removed the blanks lines for aesthetic purposes; you can put them back in if you really want them. I also added a exit /b because having a goto followed by the label you're going to is bad form. The quotes in the if statement are there to prevent the script from breaking if the user enters nothing.

Goto was unexpected at this time please

im messing around for a secret file that i can type in and just keep things to myself here is the code
#echo off
echo PLEASE ENTER THE PASSWORD TO CONTINUE
set /p password="hello"
IF %c%==hello goto top
IF NOT %c%==goto PASSERROR
:passsuccess
title matrix
color 0a
mode 1000
:top
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%
goto top
:passerror
echo try again
Your problem is that c is undefined. You are entering the password into password then checking c.
In case of spaces or no entry, use
if "%varentered%"=="somevalue" goto ...
For instance,
if "%c%"=="" goto paserror
or
if not defined c goto ...
as you have it, if %c% is not equal to goto, execute the executable passerror

What is wrong with this batch script

I am starting to get into programming and recently learned the basics of batch script. I am doing something private, but whenever I try to use an input I put to "set/p", it exits the batch.
I'd like to know if there is something wrong with it
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto c1
:y1
cls
color 0a
Before and after this there is just "echo" and text
Thanks in advance
You should use quotes around items when comparing, eg.:
if "%msg%" == "y"
Not at my computer now but four things to check / try.
Use IF /i so that it will work even if you type in CAPS. (http://ss64.com/nt/if.html )
As thebjorn said, its good to use the "" around the variable and the value.
Your example looks like it may be missing a space after the %msg%
The most likely issues based just on the code youve included is what Stephen said... In the example code, once it gets to the :y1 label would clear the screen, set color then do your echo super fast and exit.
Try the pause trick.
Thanks for the answer but I already figured it out.
I tried different changes and then I finally got it
I just needed to capitalize GOTO
#echo off
#echo [question] [message] y/n?
set /p msg=
if %msg%== y GOTO :y1
if %msg%== n GOTO c1
:y1
cls
color 0a
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto :c1
:y1
cls
color 0a
I believe your error would be your beginning. Where you haveset /p msg=, this would have %msg% equaling nothing which if your batch program starts with an error then it will close immediately.
You can have it equal anything you want for example if you want it to be blank you would have to have: set /p msg=" " and in the Command Prompt it will just take your cursor to a blank line waiting for your input.
The most standard option is what command prompt uses to indicate a new line: set /p msg=" > "
I edited the code for you and you can change it as you wish... I cleaned up what it would look like a bit for you also
#echo off
echo input y or n
echo.
set /p "msg= > "
if "%msg%"=="y" goto :y1
if "%msg%"=="n" goto :c1
:y1
cls
color 0a
pause
Edit 1: fixed my quotes in the above code... the original placing wouldn't of done much with the above by itself but with additional code it may cause problems, when it comes to quotes and spaces batch is kinda picky
Side Note 1: if you want to have a timed pause instead of it waiting for a button press when it reaches pause, you can use the command timeout /t # the # would be the number of seconds you want it to wait and you can also press any button to have it be like the pause button... now just like how the pause button has the "Press any key to continue..." phrase so with timeout it has something like this but do not quote me on this "Press any key to continue or wait #..." you can hide the phrases from appearing but using either of these code lines: pause >nul or timeout /t # >nul

Goto not working correctly in batch?

I have a few things set up in a batch game. Instead of going where it is supposed to when the sure enters an option and hits "Enter" it goes to the next thing that starts with a : (I don't know what it is called).
Instead of it going to "Youtube" when the user types "Y".
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N cls goto :youtube
if %option%==y start chrome (Censored link)
if %option%==n cls goto :youtube
:version
cls
#echo off
echo.
echo[
#echo off
echo.
echo[
echo --Version--
echo Lightup Demo
#echo off
echo.
echo[
#echo off
echo.
echo[
#echo off
echo.
echo[
pause
goto :versionwhite
:youtube
echo Would you like to visit the Creator's Youtube channel?
echo Gameplay commentarys and such.
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N goto :Beginning
if %option%==y start chrome (Censored link)
if %option%==n goto :Beginning
Essentially you were missing a command separator after the CLS but I've made some other changes such as /i case insensitive comparing and made the checking routines more robust to spaces or no input.
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if /i "%option%"=="Y" start "" chrome "(Censored link)"
if /i "%option%"=="N" cls & goto :youtube
goto :visitoption
The "thing" is called a label
Since you have no control over what the user types, you should use
if "%option%"=="Y" start chrome (Censored link)
that is, quote both sides of the comparison (this is not bullet-proof, but serves adequately where the user is not deliberately trying to break your system.)
Adding the /i switch to the if will make the comparison case-insensitive.
if defined option set "option=%option:~0,1%"
will set option to just the first character.
Note that if the user replies simply Enter then the value of the variable remains unchanged. You can use this characteristic to your advantage
set "option=defaultvalue"
set /p option=Y or N:
will set option to defaultvalue if the user replies simply Enter.
start will start a process independently. The batch simply carries on to the next statement. You are probably beter off using start "window title for this instance" ... - it's a quirk of start that the first "quoted parameter" is used as the window title where you may be expecting it to be used as a parameter.
To concatenate a series of commands in a single line, you need to separate the individual commands with an ampersand &
Once you've turned echo off once, you don't need to do it again (unless you execute echo on, which you can do during debugging to show the program flow.) The leading # means don't echo this command - without it, the initial ECHO OFF would be reproduced.
You can use call :label to execute a subroutine that starts at :label in this batch file. If you use call label then the "subroutine" executed is the executable label. This is a very important distinction.
For this reason, I eschew the use of goto :label - although it works - because the colon is not necessary and for congruence between the goto and call commands.
The one exception to this omit-the-colons approach is where the colon actually does have an effect - goto :eof very specifically means 'goto the physical end of this batch file' - the label :eof is understood by cmd to have that meaning, and should not be defined in the batch.
You've set it so that it goes to youtube when the user presses n, not when the user enters y like you said your trying to in the question:
if %option%==N cls goto :youtube
Please try seeing what the cod eyour using is doing before you post a question on it.
And SO has one of the worlds easiest CodeSlabing systems. HOW BLOODY HARD IS IT TO PRESS SPACE 4 TIMES OR HIGHLIGHT AND PRESS THE "Code Sample" BUTTON?
Mona.

Resources