0

I am trying to create a number typing game to help train me and whoever else of my coworkers to see a number and type it faster without error. So I decide to make a quick game in notepad++, because im also trying to make CYOA (choose your own adventure) stories but thats for another post. I have posted my code below.

@ECHO OFF

:START
cls
echo num="%random%"
echo.
set /p inp=
if %inp% == %num% goto rght
if %inp% neq %num% goto wrng

:rght
echo.
echo CORRRECT
pause
GOTO START

:wro
echo.
echo wrong
pause
GOTO START

when I run the bat file, the number generated by %random% prints and if you put the correct or incorrect number, i get this

'wrng' is not recognized as an internal or external command, operable program or batch file.

and then continues to :rght even though I typed in the incorrect number

What does this mean and how can i fix this?

linked below is the exact message i get after putting the incorrect value

error after input

update

i have added a timer that goes straight to the next number after input which works fine but my counters for correct and incorrect answers both stay at 0 regardless of how many are put entered right or wrong. i basically need help understanding why my 2 counters dont work

@ECHO OFF
:START
CLS
color 07
SET "CORRECT=0"
SET "INCORRECT=0"
ECHO CORRECT %CORRECT%
ECHO INCORRECT %INCORRECT%
ECHO.
ECHO.
SET "num=%RANDOM%"
ECHO %num%
ECHO.
SET /P "INP= "
IF NOT "%INP%"=="%num%" GOTO WRNG
color 27
ECHO.
ECHO CORRECT
SET /a CORRECT=CORRECT+1
GOTO TIMER

: WRNG
color 47
ECHO.
ECHO INCORRECT
SET /a INCORRECT=INCORRECT+1
GOTO TIMER

: TIMER
set /a time=1
: TM
ping localhost -n 2 >nul
set /a time=%time%-1
if %time%==0 (
goto START
)else goto TM
Slovakolarik
  • 17
  • 1
  • 5

2 Answers2

1

Your main issues are that you're not setting num to anything just echoing it and that you're GOing TO a label which doesn't exist. You can also simplify the structure a little:

    @ECHO OFF

    :START
    CLS
    SET "num=%RANDOM%"
    ECHO %num%
    ECHO.
    SET/P "INP= "
    IF NOT "%INP%"=="%num%" GOTO WRNG
    ECHO.
    ECHO CORRRECT
    PAUSE
    GOTO START

    :WRNG
    ECHO.
    ECHO WRONG
    PAUSE
    GOTO START

Edit

Here's an update based upon my understanding of your now removed edit.

@ECHO OFF

SET/A "correct=incorrect=0"

:START
CLS
COLOR 07
SET "num=%RANDOM%"
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET /P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
)
PING -n 2 0.0.0.0 1>NUL
GOTO START
Compo
  • 36,585
  • 5
  • 27
  • 39
  • It would be appreciated by myself and future searches should you accept the answer. However you appear to have completely changed your question since my answer. That should have technically been a new question. – Compo Apr 26 '17 at 21:12
  • your right. i didnt see the check mark to accept the answer. and it would be a better idea to post the second version of the code as a new question. thanks for the tip – Slovakolarik Apr 27 '17 at 00:55
  • I have updated my answer to encompass what I think you intended from your now removed edited question. – Compo Apr 27 '17 at 00:57
0

I have run you code and its works 95%. the only problem I ran into was when I input a correct answer, it added 1 to both correct and incorrect. though I love how neat and well put together you version of the code is.

all i did was move a copy of the ping line into both IF and ELSE and the code was able to add a point to the correct or incorrect score.

@ECHO OFF

SET/A "correct=incorrect=0"

:START
CLS
COLOR 07
SET "num=%RANDOM%"
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET /P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
    PING -n 2 0.0.0.0 1>NUL
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
    PING -n 2 0.0.0.0 1>NUL
)
GOTO START

i cant thank you enough compo for helping me make sense of the mess I started with

Slovakolarik
  • 17
  • 1
  • 5
  • The code with respect to the counter has not changed by writing an extra ping command and moving them into the if else blocks. It achieves nothing other than increasing batch file size and spoiling the look of the code. As an additional note, _because you didn't state your target OS_, on Vista and newer you can replace the `Ping` command line with `>Nul Timeout 1` – Compo Apr 27 '17 at 08:07