0

I am Lloyd, and I am new to Stack Overflow.

Anyways, I am writing a Batch File about Calculator. But, one thing I noticed, is that the error level always sets to 1 even when there's no error.

I also tried this command I've searched in Google:

set errorlev=0

But, that command doesn't do anything; I mean, it is still set to errorlevel 1.

So, here is my Batch File:

@echo off
cls
title Calculator
ping 1.1.1.1 -n 1 -w 1000 > nul
echo.
echo Welcome to Calculator!
ping 1.1.1.1 -n 1 -w 1000 > nul
echo.
echo Initializing ...
ping 1.1.1.1 -n 1 -w 500 > nul
echo Please wait ...
ping 1.1.1.1 -n 1 -w 3000 > nul
goto Input
:Input
cls
color 07
echo Please choose an operation.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /p "op=>"
if %op%==Addition goto Addition
if %op%==addition goto Addition
if %op%==Subtraction goto Subtraction
if %op%==subtraction goto Subtraction
if %op%==Multiplication goto Multiplication
if %op%==multiplication goto Multiplication
if %op%==Division goto Division
if %op%==division goto Division
cls
ping 1.1.1.1 -n 1 -w 500 > nul
color 0c
echo ERROR : That's not a valid operation.
echo.
ping 1.1.1.1 -n 1 -w 500 > nul
echo You have entered "%op%".
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Input
:Addition
cls
title Calculator - Addition
echo Addition:
echo.
echo  _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the first addend.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "add1=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Addition1
:Addition1
cls
title Calculator - Addition
echo Addition:
echo.
echo  %add1% + _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the second addend.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "add2=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Addition2
:Addition2
cls
title Calculator - Addition
echo Addition:
echo.
echo  %add1% + %add2% = ?
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /a "resultadd  = %add1% + %add2%"
echo The answer is %resultadd%.
rem %resultadd%
ping 1.1.1.1 -n 1 -w 2000 > nul
echo.
echo Type "Back" to enter another problem.
echo Type "Exit" to exit the Calculator.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /p "adda=>"
if %adda%=="Back" goto Input
if %adda%=="back" goto Input
if %adda%==Back goto Input
if %adda%==back goto Input
if %adda%=="Exit" goto Exit
if %adda%=="exit" goto Exit
if %adda%==Exit goto Exit
if %adda%==exit goto Exit
:Subtraction
cls
title Calculator - Subtraction
echo Subtraction:
echo.
echo  _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the minuend.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "subtract1=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Subtraction1
:Subtraction1
cls
title Calculator - Subtraction
echo Subtraction:
echo.
echo  %subtract1% - _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the subtrahend.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "subtract2=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Subtraction2
:Subtraction2
cls
title Calculator - Subtraction
echo Subtraction:
echo.
echo  %subtract1% - %subtract2% = ?
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /a "resultsubtract  = %subtract1% - %subtract2%"
echo The answer is %resultsubtract%.
rem %resultsubtract%
ping 1.1.1.1 -n 1 -w 2000 > nul
echo.
echo Type "Back" to enter another problem.
echo Type "Exit" to exit the Calculator.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /p "adda=>"
if %adda%=="Back" goto Input
if %adda%=="back" goto Input
if %adda%==Back goto Input
if %adda%==back goto Input
if %adda%=="Exit" goto Exit
if %adda%=="exit" goto Exit
if %adda%==Exit goto Exit
if %adda%==exit goto Exit
:Multiplication
cls
title Calculator - Multiplication
echo Multiplication:
echo.
echo  _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the multiplicand.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "multiply1=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Multiplication1
:Multiplication1
cls
title Calculator - Multiplication
echo Multiplication:
echo.
echo  %multiply1% x _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the multiplier.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "multiply2=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Multiplication2
:Multiplication2
cls
title Calculator - Multiplication
echo Multiplication:
echo.
echo  %multiply1% x %multiply2% = ?
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /a "resultmultiply  = %multiply1% * %multiply2%"
echo The answer is %resultmultiply%.
rem %resultmultiply%
ping 1.1.1.1 -n 1 -w 2000 > nul
echo.
echo Type "Back" to enter another problem.
echo Type "Exit" to exit the Calculator.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /p "adda=>"
if %adda%=="Back" goto Input
if %adda%=="back" goto Input
if %adda%==Back goto Input
if %adda%==back goto Input
if %adda%=="Exit" goto Exit
if %adda%=="exit" goto Exit
if %adda%==Exit goto Exit
if %adda%==exit goto Exit
:Division
cls
color 07
title Calculator - Division
set errorlev=0
echo Division:
echo.
echo  _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the dividend.
set /p "divide1=>"
ping 1.1.1.1 -n 1 -w 500 > nulping 1.1.1.1 -n 1 -w 1000 > nul
goto Division1
:Division1
cls
title Calculator - Division
set errorlev=0
echo Division:
echo.
echo  %divide1% ÷ _
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Enter the divisor.
ping 1.1.1.1 -n 1 -w 500 > nul
set /p "divide2=>"
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Division2
:Division2
cls
title Calculator - Division
set errorlev=0
echo Division:
echo.
echo  %divide1% ÷ %divide2% = ?
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /a "resultdivide  = %divide1% / %divide2%"
echo The answer is %resultdivide%.
if errorlevel 1 goto Errdiv
if not errorlevel
ping 1.1.1.1 -n 1 -w 2000 > nul
echo.
echo Type "Back" to enter another problem.
echo Type "Exit" to exit the Calculator.
ping 1.1.1.1 -n 1 -w 1000 > nul
set /p "adda=>"
if %adda%=="Back" goto Input
if %adda%=="back" goto Input
if %adda%==Back goto Input
if %adda%==back goto Input
if %adda%=="Exit" goto Exit
if %adda%=="exit" goto Exit
if %adda%==Exit goto Exit
if %adda%==exit goto Exit
:Errdiv
cls
color 0c
ping 1.1.1.1 -n 1 -w 1000 > nul
echo.
echo ERROR : Calculator cannot solve that problem.
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Division
:Exit
cls
ping 1.1.1.1 -n 1 -w 500 > nul
echo.
echo This window will now close.
echo.
ping 1.1.1.1 -n 1 -w 1000 > nul
echo Type "Okay" to exit, otherwise type "Cancel" to cancel.
ping 1.1.1.1 -n 1 -w 2000 > nul
set /p "ea=>"
if %ea%=="Okay" goto End2
if %ea%=="okay" goto End2
if %ea%==Okay goto End2
if %ea%==okay goto End2
if %ea%=="Cancel" goto Input
if %ea%=="cancel" goto Input
if %ea%==Cancel goto Input
if %ea%==cancel goto Input
cls
color 0c
ping 1.1.1.1 -n 1 -w 1000 > nul
echo ERROR : That's not a valid choice.
echo.
ping 1.1.1.1 -n 1 -w 500 > nul
echo You have entered "%ea%".
ping 1.1.1.1 -n 1 -w 1000 > nul
goto Exit
:End2
cls
ping 1.1.1.1 -n 1 -w 1000 > nul
echo.
echo Bye!
echo.
ping 1.1.1.1 -n 1 -w 500 > nul
exit`

As you can see, the Batch File is too long. (7 kb) :D

And under the Division2 Label :Division2, you will see a text if errorlevel 1 goto Errdiv

And this:

:Errdiv cls color 0c ping 1.1.1.1 -n 1 -w 1000 > nul echo. echo ERROR : Calculator cannot solve that problem. echo. ping 1.1.1.1 -n 1 -w 1000 > nul goto Division

I don't know why if I enter the dividend, then the divisor, it will go to the Label where there is error, (Errdiv).

I also tried this: %errorlevel%==1 it still go to the error thing.

Is there a way on how to get to the error label if there's an error only?

Lloyd Dominic
  • 778
  • 8
  • 25

2 Answers2

1

I won't tell you that it's quite useless to code a calculator with batch and to offer division function while cmd is not capable of dealing with floats (for CMD 5/3 is 1).

However, the point is this line: ping 1.1.1.1 -n 1 -w 1000 > nul You are pinging the unreachable ip adress 1.1.1.1 to wait 1 second. Each time you execute this command it sets the errorlevel to 1 as it can't reach the target. That's it.

To fix it you should ping localhost instead as localhost is always reachable: ping -n 2 localhost> nul

This will ping localhost 2 times with 1 second between the pings. So if you want to wait for x seconds you have to do ping -n x+1 localhost> nul. You should replace all ping 1.1.1.1 statements but don't forget to divide the desired time to wait by 1000 and to add 1.

Further, set errorlev=0 makes no sense at all. This simply sets the value of the variable errorlev to 0 but errorlev has nothing to do with errorlevel. So this is completely useless.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Thanks, @MichaelS for the answer. But, one question, can I use `set errorlevel=1`? Because I have tried this on cmd / Command Prompt. I entered `set errorlevel=1`, and `echo %errorlevel%`. Cmd says 1. Also, the reason on why did I put 1.1.1.1 in the ping command, it's because I've searched `How to wait on MS-DOS?` on Google, then I've see a command like this: `ping 1.1.1.1 -n 1 -w 1000 > nul`. That's why, I write the command ping 1.1.1.1. – Lloyd Dominic May 27 '15 at 10:28
  • Yes, you can use `SET ERRORLEVEL=1`. But you have to understand, that %ERRORLEVEL% is a system variable. It contains the "result" of the last executed command. Usually it's 0 if the everything went well. If it is greater than 0 the content tells you which error occurred. So manipulatin it by hand is only useful if you want to work with this function. But every time you change the errorlevel your value will be overwritten as soon as you execute another command. The problem with ping 1.1.1.1 is a very common mistake. This also works but sometimes (like in your case) it might cause trouble. – MichaelS May 28 '15 at 08:43
  • The IP `1.1.1.1` is now taken by CloudFlare, so this will not wait for 1s but return in some ms. – hdev Mar 22 '19 at 07:33
-1

You should have a look at this old post. It describes the difference between the file extension .bat and .cmd related to the setting of Errorlevels.

Community
  • 1
  • 1
Clijsters
  • 4,031
  • 1
  • 27
  • 37
  • This has nothing to do with bat or cmd file extention. This is simply bad code. Check my answer. – MichaelS May 27 '15 at 08:11
  • Sure. _'even when there's no error'_... Nevertheless the file extension is related to such an issue. – Clijsters May 27 '15 at 08:25
  • That does'nt have to do with the extension. MS-DOS Batch Files (.bat) and Windows NT Command Script (.cmd) are a bit same. @MichaelS is right, I should have set the ping to localhost, 'cause 1.1.1.1 is unreachable. – Lloyd Dominic May 27 '15 at 09:53