TechnologicalGenious
TechnologicalGenious

Reputation: 37

How to do math in batch-file

I have been having troubles with batch-codes that I would expect to work, but don't...

Below is what I have written...

@echo off
cls

:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.

goto :loop
Echo Unexpected Error . . .
pause
Exit

Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.

Upvotes: 1

Views: 10355

Answers (2)

JimmyG
JimmyG

Reputation: 131

The given answer didn't seem to work correctly. The second check for a valid integer doesn't do what it's supposed to do. The first input check handles all non-integer cases. Delayed expansion isn't necessary and make sure to use == for strings and EQU for numerics. Put in an exit condition too, since Ctrl-C is messy.

@echo off
setlocal enableextensions
cls

echo Just hit Enter/Return to exit

:loop
set input=
set /p "input=Input a number: "
if "%input%" == "" (
    echo exiting...
    exit/b 0
)

REM check if input valid
set /a "number=%input%"
if not "%input%" == "%number%" (
    cls
    echo Please enter an integer number
    echo.
    echo.
    goto :loop
)

Set /a Even=%number% %% 2
if %Even% EQU 0 (
    echo Substituting Even Number in: x / 2
    set /a answer=%number% / 2
) else (
    echo Substituting Odd Number in: 3x - 1
    set /a answer=%number% * 3 - 1
)
echo %answer%
echo.
echo.

goto :loop

Upvotes: 0

Claudia
Claudia

Reputation: 1269

Put a setlocal enableextensions at the beginning after the @echo off, e.g.

@echo off
setlocal enableextensions
cls

Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:

@echo off
setlocal enableextensions enabledelayedexpansion
cls

:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.

goto :loop

I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).

Upvotes: 1

Related Questions