1

So I've been working on a program in batch where it finds an integer within a user-defined range. It works by:

  1. getting the range inputted
  2. Finds halfway distance between and asks if desired # is higher
  3. Sets either the highest/lowest number as one of the parameters of the range.
  4. Rinse and repeat steps 2-4 until it finds the correct number.

CODE (WIP)

    title THE AI
@echo off
set /a attempts=0
color a
cls

:setup
::sets up the range. e.g. from 5-600
set /p mini="Enter minimum number here:"
echo Succesfully read minimum number, %mini%
set /p maxi="Enter maximum number here:"
echo Succesfully read maximum number, %maxi%

:bugfix1

cls

::Makes sure the maximum number is higher than minimum number
if %maxi% LEQ %mini% (
echo Your minimum number is either higher than your maximum, or the same.
echo Or, you put in a decimal. Shame on you. Please fix it
echo.
echo.
echo Or, in short, YOU'RE STUPID AND CAN'T FIGURE OUT NUMBERS. FIX!!! NOW!!!
pause
cls
goto setup) ELSE (
echo You are good to go!

::sets up for finding the difference
set numbig=%maxi%
set numsmall=%mini%

echo %numsmall% is the numsmall variable
echo %numbig% is the numbig variable
pause
)


:Actual_1
::Finds the difference.
set /a range = %numbig%-%numsmall%

::finds the half mark in the range and adds minimum number to it to find the true middle.
set /a newnum1 = (%range%/2)+%mini%

::sees if the number is higher or lower than given number
echo Is the desired number higher than %newnum1%? (y/n)
echo Or, if the correct answer was found, enter ppp.
set /p booleanfinder = Please enter y or n
::Activates if the number is lower than desired number.
echo %numsmall%
if /i "%booleanfinder%" == "y" goto TheDesiredNumberIsHigher

::| set attempts = %attempts%+1 | goto Actual_1  |pause 

if /i "%booleanfinder%" == "n" goto TheDesiredNumberIsLower

::  if /i %booleanfinder% == ppp (
::echo I took %attempts% to get the answer.) ELSE (
::  echo Why can't you type? 
::  echo Going back to the "Actual_1" function
::  echo Try to type better this time.)
::pause

:TheDesiredNumberIsHigher
::If the desired number is higher than that of %booleanfinder%'s
set numsmall = %newnum1%
echo %numsmall% is the lowest number possible
pause
goto Actual_1

:TheDesiredNumberIsLower

set numbig = %newnum1%
echo %newnum1% is now the highest number possible
pause
goto Actual_1

:YouGotIt

Mostly though, I'm having problems with this section

goto setup) ELSE (
echo You are good to go!

::sets up for finding the difference
set numbig=%maxi%
set numsmall=%mini%

echo %numsmall% is the numsmall variable
echo %numbig% is the numbig variable
pause
)

It appears that when I try to echo either %numbig% or %numsmall% it just gives me a blank number. (The 2 variables have been defined earlier in the code)

Any help is appreciated. Thanks for reading!

bob zhang
  • 43
  • 1
  • 6
  • please check this [URL](http://stackoverflow.com/help) it will be useful to lift you content quality up – Willie Cheng May 19 '16 at 01:27
  • `) ELSE (` needs to be on its own separate line – SomethingDark May 19 '16 at 02:44
  • 2
    Another one.... You need [delayed expansion](http://stackoverflow.com/a/30284028/2152082) – Stephan May 19 '16 at 06:03
  • 1
    @SomethingDark, I think you are wrong; multi-line `else` works fine as long as it is on the same line as the parentheses `)` and `(`... – aschipfl May 19 '16 at 09:11
  • 2
    Perhaps the `::`-style remarks cause trouble since they are placed in parenthesised blocks of code, which is not recommended, so I would try to replace them by `rem`... – aschipfl May 19 '16 at 09:12
  • If I could, I would upvote all your comments. Thanks to everyone to responded. - especially to Stephan. – bob zhang May 20 '16 at 04:11

0 Answers0