Reputation: 51
@echo off
:WriteAgain
set x=
set /p Variables=Write your expression
set /a x=%Variables%
if %errorlevel% neq 0 goto ErrorOccured
echo %x%
goto :eof
:ErrorOccured
echo.Your expression is not valid
goto WriteAgain
:eof
Greeting, It is supposed to be a simple calc, but for some reasons, when "if" works(for 1/0) it looks like "goto" doesnt(I may be mistaken here). Could you help me to solve this problem? Also I am thinking about typing error in any txt: should I use 2>txt_name.txt
after neq 0
or what?
Upvotes: 1
Views: 58
Reputation: 100
You don't put
goto :eof
Try using
goto eof
And also I am not sure but maybe the name eof is no good (Is used by CMD itself) so keep things simple and use any other name like "exit, problem, fail, etc..."
Upvotes: 0
Reputation: 73865
goto :eof
is a built-in construction to return from a subroutine (call :subroutine
). It exits current batch file when used not in a subroutine.
Rename the label to end
, for example.
Or use exit
instead of goto
to the end of batch file.
For output redirection examples and syntax see http://ss64.com/nt/syntax-redirection.html so in your case echo
prints to standard output thus >
must be used:
echo Your expression is not valid >errlog.txt
Some utilities indeed print the errors to STDERR and the standard >
won't catch the messages, so command 2>errlog.txt
should be used.
Upvotes: 1