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

how to prevent command prompt from closing after running batch file
command prompt closes immediately after opening batch file
exit batch script without closing window
batch file window not closing
prevent user from closing batch file
how to stop a batch file in command prompt
cmd automatically closes windows 10
how to keep cmd window open after running batch file

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

3 Ways To Prevent Command Prompt From Closing After Running , When we run any batch file, the Command Prompt window will 3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) Open the batch file in Notepad by right clicking on it and click on “Edit” from These all are easy and quick ways to keep your window of the  You may follow the steps below to keep the command prompt open. Open an elevated Command Prompt window. To open an elevated Command Prompt, click Start, type cmd, right-click Command Prompt, and then click run as administrator. Type the following command and press ENTER. cmd /k netstat. Exit the Command Prompt window.


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: 

Command Prompt Immediately closes (when opening directly , I am directly opening the command prompt and it closes after a split second. issue results in spam of people having issues with the prompt closing after running a bat file, I had some weird code line there saying something and ". unbiased, critical discussion among people of all different backgrounds. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent


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

Integrated Terminal in Visual Studio Code, In Visual Studio Code, you can open an integrated terminal, initially starting at the root or alter the state of an existing terminal to perform a quick command-line task. The shell , shellArgs , env , and cwd terminal settings all support resolving run another login shell, reordering the $PATH potentially in unexpected ways. Command Prompt and .bat files close instantly - posted in Windows 10 Support: Hello, I really need help. (and by the way, Im new here, so Im sorry if I do anything wrong) My command prompt and


My command prompt opens and then quickly closes before I can , I am trying to open a Python file via the command prompt, but whenever it opens, it immediately closes before I can even read anything. UAC - All Batch Files Fail when Run-as-Administrator registry key that controls the running of elevated batch files on cmd.exe. the command window opens and


Command prompt is randomly opening and closing, So I've been reading many similar occurrences, command prompt opens and closes before I can read it , and it could be anything from malware  Find answers to batch file to open cmd prompt, change directories, run command and close from the expert community at Experts Exchange batch file to open cmd prompt, change directories, run command and close Solutions | Experts Exchange


Program execution Functions - Manual, Open files with lock (especially open sessions) should be closed before proc_close — Close a process opened by proc_open and return the exit code all in a big long concatenated command line with multiple quoted-filename parameters do with the fact that only one script/process can operate at once on a session. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term "batch" is from batch processing , meaning "non-interactive execution", though a batch file may not process a batch of multiple data.