lundi 23 novembre 2015

"if" statements in batch: file crashes when the input is supposed to be a number, but instead has words with spaces

So, I have a set /p thing in a batch file. For the user to proceed, they must type "2". I can't seem to find my specific problem anywhere.

@echo off
echo "What is 1+1"

set /p oneplusone=Type Answer Here: 

if %oneplusone%==2 goto correct
if %oneplusone% neq 2 goto incorrect

:correct
cls
echo Correct!
pause
goto youranswer

:incorrect
cls
echo Wrong.
pause
goto youranswer

:youranswer
cls
echo You answered: %oneplusone%

Now, if the user types a number/word with no spaces, everything works out, and it shows your answer as wanted.

However, say if you type something like "hi there" with a space, the batch file closes immediately. This only happens when the answer needs to be a number, and you type something that involves a space.

Any help?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire