Richhh_y
Richhh_y

Reputation: 79

Unexpected exit after user input

Need a little help here for my code.

When the script detects save-file it needs to ask whether to re-create it. At this point, I click on Y for Yes, but it exits.

Here's the code:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if %choice% == "y" goto welcome
if %choice% == "n" exit
if %choice% == "Y" goto recreation
if %choice% == "N" exit

Upvotes: 2

Views: 141

Answers (2)

double-beep
double-beep

Reputation: 5519

When you are including quotes only in one side, then the quotes are included in the comparison. I wouldn't recommend neither removing the quotes from one side, which isn't safe, nor adding quotes to both sides like this (which also works good) [parenthesis included]:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if "%choice%" == "y" (goto welcome)
if "%choice%" == "n" (exit)
if "%choice%" == "Y" (goto recreation)
if "%choice%" == "N" (exit)

Instead, I would go forward and entirely change your code using the choice command doing the following:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
choice /C:YyNn /CS /M "Please enter (Y,N):" /N
if errorlevel 3 (exit)
if errorlevel 2 (goto :welcome)
if errorlevel 1 (goto :recreation)

which is much sorter.

For more info about the commands used, open a new cmd and type:

  • echo /?
  • choice /?
  • if /?

Upvotes: 1

Hayz
Hayz

Reputation: 174

Use quotes on both sides, here is an example:

BAD:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if %choice% == "y" goto welcome
if %choice% == "n" exit
if %choice% == "Y" goto recreation
if %choice% == "N" exit

GOOD:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if "%choice%" == "y" goto welcome
if "%choice%" == "n" exit
if "%choice%" == "Y" goto recreation
if "%choice%" == "N" exit

Also here I have made a better version of your code:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
choice /c ynY /cs /m "It appears that you already created the configuration file, would you like to save it"
cls
if "%errorlevel%" == "1" goto welcome
if "%errorlevel%" == "2" exit
if "%errorlevel%" == "3" goto recreation

Upvotes: 3

Related Questions