0

Below are my simple calculator batch i trying to do, however on the set /a unable to do the job. What is the problem and my mistake?

@echo off
Title Calculator
:start
echo Press 1 for "+"
echo Press 2 for Exit
echo.
set /p input="Please choose your option: "
if %input%==1 (
echo.
set /p num1="Please enter first number: "
set /p num2="Please enter second number: "
set /a ans=%num1%+%num2%
echo Your answer is %ans%
pause
cls
goto start
)
if %input%==2 (
echo.
echo Thanks!
pause
exit
) else echo Invalid input!
pause
goto start

When i first run the batch is will return me the Missing operand. When i continue again the error disappear without giving me the answer, when the third time i continue, it return me the answer of the number that i wanted to add up.

For example: 1. 10+10 result is Missing operand 2. 1+1 result is empty 3. 2+2 result is 20 (which is the 2 number i enter at first time)

Please help what my error is. Thanks.

kwan
  • 1
  • 1
    Indent the lines within parenthetical code blocks. You don't have to left-justify everything in batch scripts. Anyway, you need delayed expansion when retrieving a variable in the same loop or code block in which it was set. – rojo Feb 22 '16 at 03:07

1 Answers1

1

Here is your batch code with using delayed expansion and indents as wisely suggested by rojo:

@echo off
title Calculator
setlocal EnableExtensions EnableDelayedExpansion
:Begin
echo Press 1 for "+"
echo Press 2 for Exit
echo/
set "input="
set /P "input=Please choose your option: "
if "!input!"=="1" (
    echo/
    set /P "num1=Please enter first number: "
    set /P "num2=Please enter second number: "
    set /A ans=num1+num2
    echo Your answer is !ans!
    pause
    cls
    goto Begin
)
if "!input!"=="2" (
    echo/
    echo Thanks^^!
    echo/
    pause
    exit /B
)
echo Invalid input^^!
echo/
pause
echo/
goto Begin

Variable input is always cleared before user is asked because otherwise the user could just hit key RETURN or ENTER to keep current value of variable input.

Delayed expansion is used on checking user input against 1 or 2 in case of user enters a character which would result in a syntax error on execution with not using delayed expansion. Better would be nevertheless the usage of command choice for first user prompt.

For an expression evaluated on runtime by using set /A the environment variables can be specified directly without being expanded at all. So instead of using

set /A ans=%num1%+%num2%

or

set /A ans=!num1!+!num2!

it is enough to write

set /A ans=num1+num2

because with parameter /A command set interprets num1 and num2 automatically as names of variables.

Delayed expansion is nevertheless needed to print the result value stored in variable ans because command processor expands otherwise %ans% by nothing respectively the value of previous run on parsing the entire block starting with ( and ending with ). This can be seen on running your batch file from within a command prompt window with first line changed to @echo on for debugging.

For more details run in a command prompt window set /? or help set and read the output help pages.

start is the name of a command. Therefore it is not good to use this word as name of a label although possible.

By the way: Always use set "variable=value" and never set variable="value" as this makes a big difference, see the answers on

Mofi
  • 38,783
  • 14
  • 62
  • 115