Batch file closes after user presses enter - batch-file

The following code is supposed to do a division computation. It should ask the user to enter the first number, wait for input, and then repeat with the second number.
(Some extra info: The user is NOT allowed to enter the number 22 or 0)
So far, after you enter the first number it exits on me. I have no idea why.
Any help would be appreciated!
#echo off
cls
:START
set /p FN = Enter first number:
if %FN% == 22 GOTO EXIT
GOTO SECONDNUMBER
:EXIT
exit /b
:SECONDNUMBER
set /p SN = Enter second number:
if "%SN%" == 22 exit /b
if "%SN%" == 0 GOTO ZEROERROR
GOTO DIVIDE
:ZEROERROR
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER
:DIVIDE
set /a RESULT = %FN%/%SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START

You can try something like that :
#echo off
Title Division Computation
:START
cls
Color 0A
set /p "FN=Enter first number : "
if "%FN%" EQU "22" Exit
::*******************************
:SECONDNUMBER
Color 0A
set /p "SN=Enter second number : "
if "%SN%" EQU "22" Exit
if "%SN%" EQU "0" GOTO ZEROERROR
GOTO DIVIDE
::*******************************
:DIVIDE
set /a RESULT=%FN% / %SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START
::*******************************
:ZEROERROR
Color 0C
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER
::*******************************
And this is a bonus Calculator with a TypeWriter and Speaking Voice from me to you just for fun :)
#echo off
Title Calculator with a TypeWriter and Speaking Voice by Hackoo 2016
Color 0A & Mode con cols=80 lines=3
Call :TypeWriter "Hello, Welcome to my calculator. First off would you like to tell me your name ? "
echo(
Call :TypeWriter "So, Whats your name ? "
set /p name=
Call :TypeWriter "Ah, Nice to meet you %name% !"
::*************************************************************
:start
Call :TypeWriter "What is the first number you want to use ? "
set /p no=
Call :TypeWriter "What Operation do you want to use ? "
set /p op=
Call :TypeWriter "What is the second number you would like to use ? "
set /p no2=
Call :TypeWriter "%no% %op% %no2% "
set /a ans=%no% %op% %no2%
Call :TypeWriter " Well, That was an easy one the answer is %no% %op% %no2% = %ans% "
Call :TypeWriter " Would you like to do another calculaton ? Y(Yes) or N(No) ?"
set /p Question=
If /I "%Question%"=="Y" (goto :start) else (Goto :end)
::*************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
#cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
exit /b
::**************************************************************
:end
Exit
::*************************************************************

Related

What am i doing wrong using the Choice command with batch?

This is not the full code, since its about 1000 lines long, but here's the problem, when i come to this section of the game
choice /c abc1 /n
when i press "a" it's supposed to "medicalbag" and instead it acts like if i were to press "1" and goes back to start
when i press "b,c,1" they all go to "medicalbag".
i can't find a solution to this, i read about the command and apparently it supports these letters and numbers, when i change them out with just numbers they work just fine, but im really not sure what im doing wrong here.
:bag
cls
echo *****************************
echo a) Medical supplies
echo b) Consumables
echo c) Weaponry
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c abc1 /n
if %errorlevel% == a goto medicalbag
if %errorlevel% == b goto consumablebag
if %errorlevel% == c goto weaponrybag
if %errorlevel% == 1 goto start
:medicalbag
cls
echo *****************************
echo Bandages: %bandagecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:consumablebag
cls
echo *****************************
echo Canned food: %cannedfoodcount%
echo Purified water: %purifiedwatercount%
echo Dirty water: %dirtywatercount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:weaponrybag
cls
echo *****************************
echo a) combatknife: %combatknifecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c a1 /n
if %errorlevel% == a goto combatknifecheck
if %errorlevel% == 1 goto bag
the errorlevel of the choice keys defined using the /c switch is returned according to the keys position in the list.
with /c abc1:
keypress: errorlevels reuturned:
a 1
b 2
c 3
1 4
To get the literal keypress, you need to use a for /f loop to capture the keypress:
For /f "delims=" %%G in ('choice /n /c abc1')Do Echo(You pressed: %%G
Note - the /n switch MUST be used when using a for loop to capture the pressed key.
Here is an example of a dynamic menu system that will return the user selected value and the description of that value.
#echo off
setlocal EnableDelayedExpansion
CALL :MENU "Medical supplies" "Consumables" "Weaponry" "Back"
CALL :CHOSE
pause
goto :eof
REM ONLY FUNCTIONS BELOW HERE
:MENU
set /a "OptionNo=0"
set "choices="
FOR %%I IN (%*) do (
set /a OptionNo+=1
call :ResolveChar !OptionNo!
set "Option!retval!=%%~I"
set "choices=!choices!!retval!"
echo [!retval!] %%~I
)
GOTO :EOF
:ResolveChar
SETLOCAL
set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_startchar=%1"
CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof
:CHOSE
choice /c !choices! /cs /m "Select Your Option: " /n
call :ResolveChar %errorlevel%
set "Option=!Option%retval%!"
echo You Selected: %retval% - %Option%
GOTO :EOF

CMD opens and closes all of a sudden. (batch file coding)

This is a simple calculator:
#ECHO OFF
TITLE Simple Calculator
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
if %selection%== "1" goto add
if %selection%== "2" goto sub
if %selection%== "3" goto multiply
if %selection%== "4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
ECHO.
SET /A Ansadd=%Numadd1%+%Numadd2%
ECHO The result is: %Ansadd%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
ECHO.
SET /A Ansub=%Numsub1%-%Numsub2%
ECHO The result is: %Ansub%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
ECHO.
SET /A Ans=%Nummul1%+%Nummul2%
ECHO The result is: %Ansmul%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
ECHO.
SET /A Ansdiv=%Numdiv1%+%Numdiv2%
ECHO The result is: %Ansdiv%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
May I know what is the problem with this and the correct code?
There were too many issues to mention in the comment section so please take a look at this example against yours to see if you can identify them:
#ECHO OFF
TITLE Simple Calculator
SET /P "Selection=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF "%Selection%"=="+" GOTO Add
IF "%Selection%"=="-" GOTO Subtract
IF "%Selection%"=="*" GOTO Multiply
IF "%Selection%"=="/" GOTO Divide
GOTO :EOF
:Add
SET /P "Numadd1=Enter an addend: "
SET /P "Numadd2=Enter another addend: "
SET /A Ansadd=Numadd1+Numadd2
ECHO.
ECHO The sum is: %Ansadd%
ECHO.
PAUSE
GOTO :EOF
:Subtract
SET /P "Numsub1=Enter the minuend: "
SET /P "Numsub2=Enter the subtrahend: "
SET /A Ansub=Numsub1-Numsub2
ECHO.
ECHO The difference is: %Ansub%
ECHO.
PAUSE
GOTO :EOF
:Multiply
SET /P "Nummul1=Enter a factor:"
SET /P "Nummul2=Enter another factor: "
SET /A Ansmul=Nummul1*Nummul2
ECHO.
ECHO The product is: %Ansmul%
ECHO.
PAUSE
GOTO :EOF
:Divide
SET /P "Numdiv1=Enter the dividend: "
SET /P "Numdiv2=Enter the divisor: "
SET /A Ansdiv=Numdiv1/Numdiv2
ECHO.
ECHO The quotient is: %Ansdiv%
ECHO.
PAUSE
Ideally you should make sure that none of your variables have existing values at the start of the script and check that the entered values are compatible/validated before using them too!
Edit
You could probably make the code shorter too:
#ECHO OFF
TITLE Simple Calculator
FOR %%A IN (Operator Integer1 Integer2 Result) DO SET "%%A="
SET /P "Operator=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF DEFINED Operator (SET "Operator=%Operator:~,1%") ELSE GOTO :EOF
ECHO(%Operator%|FINDSTR /L "+ - * /">NUL||GOTO :EOF
SET /P "Integer1=Enter your starting integer: "
SET /P "Integer2=Now the other integer: "
SET /A Result=Integer1 %Operator% Integer2
ECHO(
ECHO The Answer is: %Result%
ECHO(
PAUSE
These two lines
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
should be:
set /p selection=if you want to add press 1,subtract 2,multiply 3 and divide 4:
There are a few errors in your code, including the math. Try and take a look at my code and compare it with yours.
#ECHO OFF
TITLE Simple Calculator
echo If you want to add press 1, subtract 2, multiply 3 and divide 4
set /p selection=
if "%selection%"=="1" goto add
if "%selection%"=="2" goto sub
if "%selection%"=="3" goto multiply
if "%selection%"=="4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
SET /A Ans=%Numadd1%+%Numadd2%
goto end
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
SET /A Ans=%Numsub1%-%Numsub2%
goto end
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
SET /A Ans=%Nummul1%*%Nummul2%
goto end
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
SET /A Ans=%Numdiv1%/%Numdiv2%
:end
ECHO.
ECHO The result is: %Ans%
ECHO.
ECHO Press any key to exit.
PAUSE>nul

Getting goto was unexpected at the time

I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.

Check if a string variable is empty in batch script

I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem
:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL
The program starts to loop again even if i give a string.
I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.
Any help is appreciated. Thanks
You can try something like that :
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" (
Cls & Color 0C
echo You must enter something
Timeout /T 2 /NoBreak>nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
Cls & Color 0C
echo You must enter something
choice /d y /t 2 > nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause

goto Was Unexpected At This Time {BATCH}

Im am trying to make a simple coin game in batch. The problem I have is once the delay is completed (ping) "goto was unexpected at this time" is all I get before the window closes. Any help would be appreciated!
#echo off
color f0
title Nim In Batch
::: Main Menu
:main
set /a coins=12
set /a time=0
cls
echo Nim In Batch!
echo.
choice /n /c pr /m "Play Or See Rules?"
if %errorlevel%==1 (
goto play
)
if %errorlevel%==2 (
goto rules
)
::: Rules
:rules
cls
echo Objective: Be the one to claim the last coin
echo.
echo Rules:
echo -You Play First
echo -You Must Remove 1,2 Or 3 Coins Each Turn
echo -Turns Switch Once Coins Are Removed
echo -Coins Must Be Removed In Order
echo.
echo Ready?
pause>nul
goto main
::: Game
:play
cls
echo (%coins%) Coins
echo.
choice /n /c 123 /m "How Many Coins Do You Wish To Remove?"
if %errorlevel%==1 (
set /a Coins=%coins%-1
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-3
)
cls
echo (%coins%) Coins
echo.
echo Computer Is Thinking
ping 1.1.1.1 -n 1 -w 3250 >NUL
if %errorlevel%==1 (
set /a Coins=%coins%-3
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-1
)
if %coin%==0 (
goto end
) else (
goto play
)
:end
cls
echo Computer Wins!
echo He Collected The Final Coin!
echo.
pause
goto main
You forget an s in %coins% here :
if %coins%==0 (
goto end
) else (
goto play
)

Resources