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 Mathso variable. Change them all to use quotes around both sides of the equality, such as with:
if "%Mathso%" == "+" GOTO run
and they should work better.
The reason for this is that cmd is 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:
if "+" == + then ...
and "+" is not equal to +.
By putting quotes on both sides, it becomes:
if "+" == "+" then ...
which is true.
And, as Aacini points out in the comment, your calculation set uses the wrong variable for the operator. You should change:
set /a Mathsans=%Mathsa%%Mathsp%%Mathsb%
into:
set /a Mathsans=%Mathsa%%Mathso%%Mathsb%
^
|
see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With