Home > Enterprise >  When I enter nothing my program says I dont want eat it, but it should text wanna eat. What's p
When I enter nothing my program says I dont want eat it, but it should text wanna eat. What's p

Time:10-07

@echo off
Set /p input="what's student's favourite dish.."
if "%input%" equ "" goto B
if "%input%" equ " " goto B
if %input%==apple goto A
if not %input%==apple goto C
goto exit
:A
echo Delicious!
goto exit
:C
echo I don't want eat it!
goto exit
:B
echo Wanna eat!
goto exit
:exit
pause

CodePudding user response:

When you don't provide a value for a variable but it's already set from a previous time that you ran the script, the interpreter will use the old value instead. The very first time that you run this code, if you enter nothing, then "Wanna eat!" will correctly be displayed.

To ensure that the variable doesn't have a value from a previous run, just unset it before the user provides a value.

@echo off
set "input="
Set /p input="what's student's favourite dish.."
if "%input%" equ "" goto B
if "%input%" equ " " goto B
if "%input%"=="apple" goto A
goto C

:A
echo Delicious!
goto exit

:C
echo I don't want eat it!
goto exit

:B
echo Wanna eat!
:exit
pause
  • Related