Home > front end >  Batch file computing two numbers
Batch file computing two numbers

Time:11-09

I am new to programming and I am having a problem on my batch file.

We were tasked to compute two numbers' difference and if the result is negative, it will compute x y; if the result is zero, it will compute 2x 2y; if the result is positive, it will compute x*y.

@echo off

set /p x=Enter the value of X: 
set /p y=Enter the value of Y: 
set /a diff=%x%-%y%

if diff LSS 0 (goto neg)
if diff EQU 0 (goto zer)
if diff GTR 0 (goto pos)

:neg
echo %diff% is a negative number
set /a R=%x% %y%
echo R = X   Y where X = %x% and Y = %y%
echo R = %x%   %y% = %R%
echo R = %R%

:zer
echo %diff% is equal to 0
set /a R1=2*%x% 2*%y%
echo R = 2X   2Y where X = %x% and Y = %y%
echo R = 2(%x%)   2(%y%) = %R%
echo R = %R1%

:pos
echo %diff% is a positive number
set /a R2=%x%*%y%
echo R = X * Y where X = %x% and Y = %y%
echo R = %x% * %y% = %R2%
echo R = %R2%

pause

when I tried to run and input 1 and 2, which will surely result to a negative. My code displayed the result as positive and it also doesn't detect it if it's == 0 if I input numbers that will result to 0.

Please tell me what I have to change. Thank you.

CodePudding user response:

Tip:- You can try to debug your batch script by removing the @echo off command, which will show you each step that the script takes(i.e. How exactly is the code being executed)

There are two mistakes in your code:

1.In the third paragraph when you are trying to compare the value of diff variable in the if statement, you forgot to surround it with "%" (i.e. %diff%).

Which made the if statement compare the "diff" as a string, not its value, to 0. Hence, no matter what value you provided, the if statement would always render the false result, therefore not executing any given statements (i.e. not jumping to the any labels).

2.And for the second issue, you should know that the script is executed line by line. Meaning once a statement is executed, it'll jump the next. Suppose you gave the value of 10 and 5 as the inputs, then there seems to be no problem, because after the diff is greater than 0, it jumps to the :pos label and gradually the code below the label ends exactly with the script.

But if you gave the inputs something like (5 and 10) or (10 and 10), it jumps to the labels :neg and :zer respectively. But since you haven't specified a statement that tells the script to stop running after the block of code below these two labels, all the statements below these two labels are executed.

So, the solution to this issue would be to end the script after the execution of the block of code below these two labels(:neg and :zor). And you can do so by adding exit /b.

Below is your code rectified with the above methods :

@echo off
set /p x=Enter the value of X: 
set /p y=Enter the value of Y: 
set /a diff=%x%-%y%

if %diff% LSS 0 (goto neg)
if %diff% EQU 0 (goto zer)
if %diff% GTR 0 (goto pos)

:neg
echo %diff% is a negative number
set /a R=%x% %y%
echo R = X   Y where X = %x% and Y = %y%
echo R = %x%   %y% = %R%
echo R = %R%
pause
exit /b

:zer
echo %diff% is equal to 0
set /a R1=2*%x% 2*%y%
echo R = 2X   2Y where X = %x% and Y = %y%
echo R = 2(%x%)   2(%y%) = %R%
echo R = %R1%
pause
exit /b

:pos
echo %diff% is a positive number
set /a R2=%x%*%y%
echo R = X * Y where X = %x% and Y = %y%
echo R = %x% * %y% = %R2%
echo R = %R2%
pause
  • Related