0
:Calcu
color 4D
mode 250
echo -----------------------------------------------------------------------------------------------
echo Welcome To The Calculator
echo -----------------------------------------------------------------------------------------------
echo "------>You have the option do Adding Subtraction Multiplication and Division just type<-------"
echo "------>the question and it will give you the answer for each of the type<-------"
echo -----------------------------------------------------------------------------------------------
echo Type 1 For Addition, 2 For Subtraction, 3 For Multiplication And 4 For Division:
@set /p Type=
IF /I %Type%==1 (
echo Addition
@set /p First = Enter First Number:
@set /p Second = Enter Second Number:
@set /a Sum = %First% + %Second%
echo %First% + %Second% = %Sum% 
)

IF /I %Type%==2 (
@set /p First = Enter First Number:
@set /p Second = Enter Second Number:
@set /a Sub = %First% - %Second%
echo %First% - %Second% = %Sub%
)
IF /I %Type%==3 (
@set /p First = Enter First Number:
@set /p Second = Enter Second Number:
@set /a Multi = %First% * %Second%
echo %First% X %Second% = %Multi%
)
IF /I %Type%==4 (
@set /p First = Enter First Number:
@set /p Second = Enter Second Number:
@set /a Div = %First%  / %Second%
echo %First% รท %Second% = %Div%
)
IF /I %Type%=>5 (
echo Not a Valid Number
)
pause
goto Calcu

Can Anyone tell me why i am getting a missing operand error Im trying to make this calculator work but its not working The Math operations worked in another code block but not here

  • Remove the `@` from all the `set /A` command to see which of those is failing... โ€“ aschipfl Sep 18 '15 at 22:45
  • You need to apply [delayed expansion](http://ss64.com/nt/delayedexpansion.html), or simply use `set /a "Sum = First + Second"` because the `set /A` allows you to do arithmetic with environment variable values without having to type all those `%` signs to get their values (read entire `set /?`). Delayed expansion is necessary for other commands in a parenthesized block, e.g. `echo !First! - !Second! = !Sub!` command. โ€“ JosefZ Sep 18 '15 at 23:14
  • 1
    You stated an invalid comparison operator: `IF /I %Type%=>5` does not work; to get a greater-than-or-equal-to comparison, use `IF /I %Type% GEQ 5`; type `if /?` for more info.... โ€“ aschipfl Sep 19 '15 at 09:29

1 Answers1

1

when setting varaibles, do not use spaces around the =, otherwise the space(s) will be part of the variable name or value.

set /p First = Enter First Number:
echo %first%
echo %first %
set /p "First=Enter First Number: "
echo %first%

as for the delayed Expansion, JosefZ mentioned:

if A==A (
  set /p First = Enter First Number:
  echo %first% - !first!
  echo %first % - !first !
  set /p "First=Enter First Number: "
  echo %first% - !first!
)
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91