The following code is not working. Whatever I input just returns an error, and then goes back to Retry.
@echo off
:maths
set /p Mathsa="first number? "
echo your first number is %Mathsa%
:retry
set /p Mathso="operator?(+-*/) "
if "%Mathso%" ==* GOTO run
if "%Mathso%" ==/ GOTO run
if "%Mathso%" ==+ GOTO run
if "%Mathso%" ==- GOTO run
echo Error, wrong operator. & goto retry
:run
set /p Mathsb="Second Number? "
set /a Mathsans=%Mathsa%%Mathsp%%Mathsb%
echo %Mathsans%
pause
I have absolutely no idea why it’s doing this. It’s for a school project and any help you can give would be appreciated!
It’s the quotes around your
Mathsovariable. Change them all to use quotes around both sides of the equality, such as with:and they should work better.
The reason for this is that
cmdis not quite the same as UNIXy shells. The quotes are preserved on the left hand of the equality so that what you end up with is:and
"+"is not equal to+.By putting quotes on both sides, it becomes:
which is true.
And, as Aacini points out in the comment, your calculation
setuses the wrong variable for the operator. You should change:into: