im trying to make a simple game and the code isnt working

May 6, 2013 at 08:02:46
Specs: Windows 7
i have know script code for a while now and i want to put it to good use but when i made my code and checked it over and over again i cant seem to find what is wrong, no matter what i put into the input box and hit enter i get this = 1 was unexpected at this time here is the code:
@ echo off
echo Welcome My Math Game
pause
:Start
cls
title My Game
echo Main Menu
echo.
echo Please Select your Level of Difficulty
echo.
echo Easy = 1
echo.
echo Close = 2
echo More Coming Soon
echo.
set /p diff=
echo.
if %diff% equ 1 goto Diff1
if %diff% equ 2 goto End
if %diff% neq 1 goto End1
:diff1
echo 1 works
pause
exit
:End
echo 2 works
pause
exit
:End1
echo end1 works
pause
exit

See More: im trying to make a simple game and the code isnt working

Report •

#1
May 6, 2013 at 10:17:57
I, too, occasionally want to do a complex program in batch script. Then I punch myself in the face until I've sated my masochistic desires. I walk around, confident that this throbbing pain would have been worse if I had used batch, instead of some sane language.

As for you, I was unable to replicate your exact error message with the code provided.

How To Ask Questions The Smart Way


Report •

#2
May 6, 2013 at 10:23:50
maybe my cmd is broken

Report •

#3
May 6, 2013 at 10:45:38
Doubtful. Chances are you're either not copy/pasting the real code, or something's changing when you copy/paste it here.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
May 6, 2013 at 12:00:04
@razor: Ha ha! yes, I remember burning up hundreds of hours trying to do some simple things with batch when I started out. But, I can understand its appeal because it appears fairly simple, (like maybe about a dozen verbs to learn, then their modifiers, then syntax) but this deceptive appearance lures many to their doom.

@OP At any rate, that said, your code suffers from three things (so far) that I can see: a) you don't have "setlocal", so your variables will persist
b) you didn't clear or pre-set the var. "diff" prior to the "set /p" statement,
c) you didn't test for diff as undefined prior to all other testing
and optionally, d) you could also use quotes to avoid the null
@ echo off
setlocal
echo Welcome My Math Game
dot dot dot ...
echo Main Menu
:: clear it here, incase it is later made into a loop above main menu
set diff=0
echo Please Select your Level of Difficulty
dot dot dot...
set /p diff=
echo.
if %diff% equ 1 goto Diff1
if %diff% equ 2 goto End
if %diff% neq 1 goto End1
dot dot dot ...

::==== OR:
set diff=
echo Please Select your Level of Difficulty
dot dot dot...
set /p diff=
echo.
if not defined diff goto :end1
if %diff% equ 1 goto Diff1
if %diff% equ 2 goto End
if %diff% neq 1 goto End1
dot dot dot ...

::==== end snippets

Note that instead of presetting diff to zero, you could "undefine" it, but then you would need two tests instead of just one for "%diff% neq":

and then there's also the quotes option, but then you have to deal with if the player includes a space in his input:
if "%diff%" equ "1" goto diff1
but if the player enters:" 1", or "1 " it won't work like you probably would want it to. you could ALSO follow the set /p with:
set /a diff+=0
which would also work unless you need diff to be alpha-capable.
Usually it's easiest just to use the variable as a label:
set /p diff=
goto :%diff% 2>nul
goto :end1
:1
echo 1 works
:2
echo 2 works

So, in short, there's about a million ways to do things that will avoid your error.


Report •

#5
May 6, 2013 at 13:29:47
I know! There's more exceptions than there are rules! And the rules interact in unintuitive ways! And the error checking is mostly non-existent! And what error messages you do get are unhelpful! And it doesn't have proper functions! And it converts a variable's name to its contents before the logic pass! And it doesn't have a debugger! And everyone's obsessed with using @echo off, but no one actually knows what it does! It is the worst language out of Windows' 4 possible choices.

nbrane: a) you don't have "setlocal", so your variables will persist
They would have if he didn't call exit, but he does so the point is moot.

How To Ask Questions The Smart Way


Report •

#6
May 6, 2013 at 19:08:44
Ha ha! yes, you have a point about language choices. As for the setlocal, I was looking "down the road" when/if the code were made to exit via "goto :eof" - I just always use it out of habit unless I know the var.s need to persist for some reason.

Report •

Ask Question