0

I have some problems with batch file. The file should accept input of 2 numbers from user and then accept input of sign. After that compare users entered sign with "+" sign, however it's not working.

Maybe you will be able to understand better from my code:

echo please enter first number
set /p number0=

echo please enter second number
set /p number1=

echo enter one of the numbers - ^+ - / ^*
set /p symbol="set the variable: "

echo %symbol%
set plus=+
echo %plus%
pause

if /i "%symbol%" EQU "+"(
    echo your choice is to sum up the numbers
    pause
    set /a answ = %number0% + %number1%
    echo answer:
    echo %answ%
)

I already tried using if /i %symbol% == plus, tried to use quotes in any combination I was able to imagine. I tried to assign operator plus variable like this set /p plus="+" then using "" around symbol variable, and so on.

But still nothing works, after I reach part where variable should be compared with symbol + my batch file just crashes. All variables are assigned correctly.

That's the link to see full file if it's necessary, however please note that full file isn't written in English language: https://1drv.ms/u/s!Ap4-t2P-Igzihb5shQZLNkKySEWOrQ.

halfer
  • 18,701
  • 13
  • 79
  • 158
  • the `/i` in `if /i "%symbol%" EQU "+"` is useless because there's no letters in the expression and a case sensitive search would work – phuclv Dec 19 '17 at 09:35
  • When writing questions here, in general you do not need `
    ` line breaks - paragraphs are much more readable.
    – halfer Dec 19 '17 at 14:57
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 19 '17 at 14:57

2 Answers2

1

I have found a way to do same thing without if statement. It turns out I can just do this:

set /p number1=
set /p number2=
set /p symbol=
set /a answ=%number1%%symbol%%number2%
halfer
  • 18,701
  • 13
  • 79
  • 158
  • With `Set /A` you don't usually need to use the variable characters, (**`%`** or **`!`**); e.g. `Set /A result=integer1 operator integer2`. – Compo Dec 19 '17 at 10:19
1

I suggest this code:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set Number1=
set /P "Number1=First number:   "
set Number2=
set /P "Number2=Second number:  "

:EnterOperator
set Operator=
set /P "Operator=Math. operator: "
if not defined Operator goto EnterOperator
if "!Operator!" == "+" goto EvaluateExpression
if "!Operator!" == "-" goto EvaluateExpression
if "!Operator!" == "*" goto EvaluateExpression
if "!Operator!" == "/" goto EvaluateExpression
goto EnterOperator

:EvaluateExpression
set /A Result=Number1 %Operator% Number2
echo The result is:  %Result%
endlocal

Read the help output on running a command prompt window set /? carefully regarding to arithmetic expressions. On using just the environment variable names Number1 and Number2 as it is possible in an arithmetic expression, the string entered by the user can be also nothing or not an integer number in which case Windows command interpreter replaces the not existing environment variable or the invalid string by value 0 on evaluation of the arithmetic expression.

The arithmetic symbol respectively operator must be evaluated using delayed environment variable expansion before evaluating the arithmetic expression as otherwise Windows command interpreter could output an error message because of an invalid operator or something completely different is done depending on what the user inputs on prompt for the operator.

Please note that a division by zero is still possible by this code.

See answer on Use IF statement on variables with unacceptable symbols (e.g. / or :) for the reason using delayed environment variable expression on comparing the operator strings before using in arithmetic expression with expansion before executing this command line. See also Why is no string output with 'echo %var%' after using 'set var = text' on command line? with general information about SET syntax.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?
Mofi
  • 38,783
  • 14
  • 62
  • 115