checksumHashi
checksumHashi

Reputation: 93

Batch Programming = "set /p" wont accept spaces as an answer

I know that there is already an answer to the exact question that i am asking, but i tried to understand that answer, or contact the person that made the answer, but i didnt get far.

What im talking about is here:

batch "set /p" crashes when no input

But i dont understand how to fix my code from the answers there.

The problem with the code below is that when i press 1, it goes to 1answered, when i press 2, it goes to 2answered, when i press just enter it goes to the start of the code, and that is ok, BUT when i type a space and then i press enter, the code crashes. When answering, please keep in mind that im a kid and dont know much about code at all, so i may not understand a complex answer.

PS sorry if i misspelled something, english is not my first language.

Example of code:

@echo off
:start
cls
set input=x

set /p input=Enter input:

if %input%==1 goto 1answered
if %input%==2 goto 2answered

echo A mistake was made
pause >NUL
goto start

:1answered
echo You entered the number 1
pause >NUL
exit

:2answered
echo You entered the number 2
pause >NUL
exit

Also, here is the code that im working on. I know its bad, but i dont know any better :/

@echo off

(
set /p usernameofplayer=
)<usernameofplayer.txt

set input=x

mode 85,30

color 0b

title LiteTec.inc

goto mainpage

::The code on top of mainpage is just for looks, random.

:mainpage
cls
echo.
echo var dog = ( -0.19 ) ^+ Jon / ( 0.15 ) ^* -foo(y,x,rule(destroy() ^* ROWS,920.47))
echo            /^* terminals used ^*/
echo       var num, id_var, id_func, id_idx, binaryOP, quote, assignOP;
echo this.getRandomRule = function () {
echo                if (this.rules.length == 0) {
echo                     return ""
echo       WELCOME TO
echo       _      ____  ______    ___ ______    ___    __ 
echo      ^| ^|    ^|    ^|^|      ^|  /  _]      ^|  /  _]  /  ]
echo      ^| ^|     ^|  ^| ^|      ^| /  [_^|      ^| /  [_  /  / 
echo      ^| ^|___  ^|  ^| ^|_^|  ^|_^|^|    _]_^|  ^|_^|^|    _]/  /  
echo      ^|     ^| ^|  ^|   ^|  ^|  ^|   [_  ^|  ^|  ^|   [_/   \_ 
echo      ^|     ^| ^|  ^|   ^|  ^|  ^|     ^| ^|  ^|  ^|     ^\     ^|
echo      ^|_____^|^|____^|  ^|__^|  ^|_____^| ^|__^|  ^|_____^|^\____^|
echo.
echo         For help type "help" and click enter
echo.
goto command




:command
set /p input=#%usernameofplayer%^>

if %input%==reset goto mainpage
if %input%==help goto help
if %input%==chat goto chat
if %input%==access goto accessnotspecified
if %input%==x goto command
if %input%==ELSE goto command



:help
echo.
echo ================================================================
echo To use a command, type in its name and click enter
echo To learn sub commands of a command, type the command
echo with [] at the end
echo example: command[]
echo To use a sub command of a command, type the sub command
echo in the [] of the command
echo List of commands available:
echo =reset
echo =chat
echo =access
echo =
echo ================================================================
goto command




:accessnotspecified
echo.
echo What to access is not specified
echo.
goto command

Upvotes: 0

Views: 380

Answers (1)

Compo
Compo

Reputation: 38623

I'd suggest changing the command section to this:

:command
Set "input="
Set /P "input=#%usernameofplayer%>"

If /I "%input%"=="reset" GoTo mainpage
If /I "%input%"=="help" GoTo help
If /I "%input%"=="chat" GoTo chat
If /I "%input%"=="access" GoTo accessnotspecified
GoTo command

…as you can see, when doublequoting the input prompt there's no need to escape >.


As a side note you can also change some of the the mainpage section to:

Echo var dog = ( -0.19 ) + Jon / ( 0.15 ) * -foo(y,x,rule(destroy() * ROWS,920.47))
Echo            /* terminals used */

…because there's no need to escape + or *.

Upvotes: 1

Related Questions