1

What is supposed to happen is that you input a number between 1 and 1,048,567. The program checks if your input is actually a number between 1 and 1,048,567. If your input is a valid number then it will continue onto the next bit of code. If the input is invalid then it will display a message saying it is invalid then loop back and ask you for input again. When I run this however, I input anything and it says invalid input even if I did input a number between 1 and 1,048,567.

Code:

:setup
@echo off
title Pokemon Shiny Sim
set delay = nul
set count = 0
set chance = 4096

:settings
:setChance
cls
echo Set shiny chance (1 in x). Range: (1-1,048,567)
echo Leave blank for 1 in 4096.
set /p chance = Input: 
set /a chance = %chance%+0
if %chance% GEQ 1 (
    if %chance% LEQ 1048567 (
        goto setDelay
    )
)
echo Invalid Input.
pause
goto setChance
:setDelay
cls
echo Set delay between attempts in seconds. Range: (1-60).
echo Leave blank for no delay.
set /p delay = Input: 
set /a delay = %delay%+0
if %delay% == nul (
    goto loopStart
)
if %delay% GEQ 1 (
    if %delay% LEQ 60 (
        cls
        goto loopStart
    )
)
echo Invalid Input.
pause
goto settings

:loopStart
set /a count = %count%+1
set /a rand = %random% %% %chance%+1
if %rand% == 1 (
    echo Attempt: %count% | Shiny: Yes!
    pause
)
else (
    echo Attempt: %count% | Shiny: No
)
goto loopStart
Finn Perry
  • 67
  • 1
  • 7
  • 2
    You are mis-setting a variable: you are setting a variable `chance `.(Note the space!) Remove the space between the equal signs –  Aug 06 '17 at 03:41
  • 1
    Also, you are `echo`ing a pipe `|` without escaping. Using `^|` instead of `|` –  Aug 06 '17 at 03:42
  • @SteveFest Thanks for the tips on the miss-set variable and the echo-ing of |. The original problem is still there though. – Finn Perry Aug 06 '17 at 06:17

2 Answers2

0

I haven't tested the code yet, but I found some fatal issues in the code.


Mis-setting variable

set /a count = %count% + 1

This sets a variable count (Note the space!). Remove the space! Also, this can be shortened to set /a count+=1.


ECHOing special characters

| is one of the special characters reserved for redirection in batch. To properly echo it, use echo string ^| string instead.


Poor IF statement practice

if %rand% == 1 (

only works when %rand% is alphanumeric. If %rand% is space, the cmd.exe sees:

if == 1 (

which is incorrect.

To correct it, do

if "%rand%"=="1" (

Alternatively, use EQU for numeric comparison, and == for string comparison.

  • 1
    I'd never recmmend using it, but even `set /a "count = %count% + 1"` wont't put a space into the variable name. Try it! It's only set without the `/a` with this behaviour. –  Aug 06 '17 at 05:02
  • @LotPings that's interesting, I will try it out soon –  Aug 06 '17 at 05:03
  • I updated my code with your suggestions and it still has the same problem where it says any input is invalid. – Finn Perry Aug 06 '17 at 06:15
0

I suggest to read first debugging a batch file and second the answer on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?

Next look on your rewritten code below:

:setup
@echo off
title Pokemon Shiny Sim
set "delay=0"
set "count=0"
set "chance=4096"

:settings
:setChance
cls
echo Set shiny chance (1 in x). Range: (1-1,048,567)
echo Leave blank for 1 in 4096.
set /P "chance=Input: "
set /A chance+=0

if %chance% GEQ 1 if %chance% LEQ 1048567 goto setDelay

echo Invalid input.
pause
goto setChance

:setDelay
cls
echo Set delay between attempts in seconds. Range: (1-60).
echo Leave blank for no delay.
set /P "delay=Input: "
set /A delay+=0

if %delay% == 0 goto loopStart
if %delay% GEQ 1 if %delay% LEQ 60 cls & goto loopStart

echo Invalid input.
pause
goto settings

:loopStart
set /A count+=1
set /A rand=%random% %% chance + 1
if %rand% == 1 (
    echo Attempt: %count% ^| Shiny: Yes!
    pause
) else (
    echo Attempt: %count% ^| Shiny: No
)
goto loopStart

All spaces around the equal signs are removed in this batch code.

The command line set "delay = nul" is modified to set "delay=0" because the condition if %delay% == nul is never true after execution of set /a delay = %delay%+0 resulting in execution of set /a delay = nul + 0 which results in assigning value 0 to environment variable delay on nul not existing as environment variable with that name having an integer value. The result of a valid arithmetic expression is always a number assigned as string to the environment variable and never a string like nul.

set /a chance = %chance%+0 is modified to set /A chance+=0 and set /a delay = %delay%+0 is modified to set /A delay+=0 because otherwise the input check is insecure as the user has for example the freedom to enter | for variable chance resulting in execution of command line set /a chance = |+0 which cause an unexpected exit of batch file execution.

Never use %variable% or !variable! in an arithmetic expression as not needed in general.

The help output on several pages on running set /? in a command prompt window explains in chapter about usage of set /A that each string which can't be interpreted as number or operator is interpreted automatically as name of an environment variable whose current value should be converted to an integer on evaluation of the expression. If the environment variable is not defined at all or its value can't be successfully converted to a 32-bit signed integer, it is replaced in the expression by integer value 0.

There are exceptions like the usage of a random number in an arithmetic expression which requires %random% or !random! or when a variable name contains a space character or a character which would be interpreted as operator. In such cases it is necessary that the Windows command interpreter replaces the environment variable name already in preprocessing state or immediately before execution of the command set by random value respectively value of the environment variable.

set /a chance = %chance%+0 makes it also possible that the user of this batch file enters for example PROCESSOR_LEVEL or PROCESSOR_REVISION and this input although not being a number at all would be handled as valid because those two strings are the names of environment variables having numbers as values. PROCESSOR_REVISION has by default a hexadecimal number assigned which can be processed nevertheless completely or partly as number by command set.

Another syntax error is in block

if %rand% == 1 (
    echo Attempt: %count% | Shiny: Yes!
    pause
)
else (
    echo Attempt: %count% | Shiny: No
)

The keyword else must be on same line as the closing ) of true branch of the IF condition separated from ) with a space character.

And redirection operator | must be escaped with caret character ^ to be interpreted as literal character to output into console window.

Note: set /A chance+=0 makes it still possible to enter for example 170 percent or 170X which results in chance having value 170 and therefore input is valid although in real the entered string is not a number.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cls /?
  • echo /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • title /?
Mofi
  • 46,139
  • 17
  • 80
  • 143