Goto was unexpected at this time? - batch-file

I am trying to make a little connect four game and whenever I hit enter without typing anything into the console it says that goto was unexpected at this time All I want it to dos go back to :X for now if there is no user input. Any help would be aappreciated.
:X
cls
echo --------------
echo Connect Four X
echo --------------
echo.
echo CHOOSE A COLUMN
echo.
echo.
echo 1 2 3 4 5 6 7
echo.
echo (%a6%) (%b6%) (%c6%) (%d6%) (%e6%) (%f6%) (%g6%)
echo.
echo (%a5%) (%b5%) (%c5%) (%d5%) (%e5%) (%f5%) (%g5%)
echo.
echo (%a4%) (%b4%) (%c4%) (%d4%) (%e4%) (%f4%) (%g4%)
echo.
echo (%a3%) (%b3%) (%c3%) (%d3%) (%e3%) (%f3%) (%g3%)
echo.
echo (%a2%) (%b2%) (%c2%) (%d2%) (%e2%) (%f2%) (%g2%)
echo.
echo (%a1%) (%b1%) (%c1%) (%d1%) (%e1%) (%f1%) (%g1%)
echo.
echo ---------------------------
echo.
echo.
set /p xchoice=:
if %xchoice% == 1 goto xcheck1
if %xchoice% == 2 goto xcheck2
if %xchoice% == 3 goto xcheck3
if %xchoice% == 4 goto xcheck4
if %xchoice% == 5 goto xcheck5
if %xchoice% == 6 goto xcheck6
if %xchoice% == 7 goto xcheck7
goto X

If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
IF syntax is if string1 operator string2 action.
You need
if "%possiblyemptyvariable%"=="1" goto ...

Related

How To Set Variable Value In If

Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)

Why does the command GOTO atack1 not result in executing the code below line :attack1 on batch file execution?

I've started to learn batch/shell a while ago. and I started to make a text based rpg called bpg. anyways, I poured a day into making the basic structure of it and then I found out the cmd closes when I use the attack trigger. I ran over it a million times, but can't find anything wrong with it. Please help.
The batch file can be downloaded from https://www.dropbox.com/s/f8r7cs0tvo8qhs8/bpg.bat?dl=0
Here is the code:
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
title BPG 1 A batch of monsters
setlocal enabledelayedexpansion
color 2
:menu
cls
echo ______ _______ _______
echo I ___ \ I ____ I I ____ \
echo I I I II I II I I \/
echo I __ I I _____I I I ____
echo I I \ \ I I I I \_ I
echo I I___I II I I I___I I
echo I______/ I_/ I_______I A BATCH OF MONSTERS
echo.
echo.
echo 1) begin
echo.
echo 2) exit
echo.
set /p c=bpg:
if "%c%" == "1" goto new
if "%c%" == "2" exit
goto menu
:new
set health=100
set enemyhealth=30
set playerdmg=10
set monsterdmg=10
goto home
:home
cls
echo --------------------
echo Welcome to BPG
echo --------------------
echo (noobs can f off)
echo.
echo 1) Battle
echo 2) Menu
echo.
set /p c=bpg
if "!c!" == "1" goto encounter1
if "!c!" == "2" goto menu
goto home
:encounter1
cls
echo --------------------
echo You: %health%
echo Enemy: %enemyhealth%
echo --------------------
echo.
echo 1) attack
echo.
echo 2) flee
echo.
set /p c=bpg
if "!c!" == "1" goto atack1
if "!c!" == "2" goto home
goto encounter1
:attack1
set /a !health!-=!monsterdmg!
set /a !monsterhealth!-=!playerdmg!
if !monsterhealth! lss 0 goto win
goto encounter
:win
cls
echo.
echo --------------------
echo You Win!
echo --------------------
echo _.oood"""""""booo._
echo _.o"" _____ . ""o._
echo oP" _.ooo"""" """"oIo._. "Yo
echo o8 oP _.-": I I"._. .8o
echo d' o8',-" : I I/ ,\. .b
echo d' d.-" : I I: (( .\
echo 8' d' """"": : I I II\_/. .8
echo 8 8' : : I) _ I II I.I 8
echo ,8 8 : : /)I \ II I\_II I I8 8.
echo 8' ,8 : : " /_) I.:' I I I I8. .8
echo 8 8' : : _ _-' \ ' __ __ 8
echo 8 8 : : \I__ I I I I I 8I 8
echo 8 8. : : II I I I-:' I 8I 8
echo 8. .8 / I __/ I__ I__I I \ I__I,8
echo 8 8 .' \ / __ . . . . . .8LL8'
echo 8 8.' -. ( ,' .. I I ,-I8 8
echo 8.(__________dd_) \__/ ' 0I...: I: (8 ,8
echo Y. Y. I :/I I,\I. .P
echo Y. "8. .,o I I I,I". ,P
echo "8. "Yo_ I IpI". ,8"
echo "Y_ "ooo.__ __.ooI". . _P"
echo '"oo_ """"" . _oo""'
echo ."""boooooood"""'.
echo.
echo.
pause
if "!c!" == "1" goto atack1
Look for the million-and-first time at this command.
Now - why wouldn't it reach the label ":attack1"
And if you ever reach that label,
set /a !health!-=!monsterdmg!
will subtract the contents of monsterdmg from the variable whose name is (the contents of health)
This error is repeated on other lines.
What's the obsession with !var!? You only need !var! when you are dealing with values that are changing within a code block - normally a loop. The syntax works at other times, but beware - it may have unforseen side-effects. Better to stick with %var% unless you need to use the !var! form.

Assistance with nested if statements

I'm new to stackOverflow. I have a question. Why is my code not working...it's nested if statements, that should work...I'm stumped. You are supposed to have money, a home (type 1 to 3) and phone. The phone is the problem. callLine is used to tell which line will be displayed each time callText is called. It adds 1 to itself every time it is called in Calling2. Whenever it gets to a certain number, it changes callText (the text displayed) to something else. But with callWho variable (work or home) it needs to be checked aswell. Here it is:
# echo off
set money=10000
set house=1
set item=Phone
set /a callLine=0
setlocal enabledelayedexpansion
echo Hello there! I am Joe, and I am the mayor of Redcrest Town.
pause
cls
echo I'm so happy to see another resident move into the Redcrest Town.
echo So, will you do me a favour and tell me your name?
set /p Name=
cls
echo Well then, %name%! Welcome!
pause
cls
echo I hope you have a great time here. You have 10,000 dollars, and a small house.
echo Please enjoy your time here!
pause
cls
:UsePhone
echo Money: %money%
echo.
echo You are using your phone. This will cost money, each time you call.
echo.
echo.
echo __i [CONTACT LIST]
echo ^|---^| [1: HOME ]
echo ^|[_]^| [2: WORK ]
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
set /p callWho=Select Number:
if %callWho% == 2 goto Calling
echo %callText%
pause
:Calling
cls
echo Calling......
ping localhost -n 4 >nul
:Calling2
if callLine == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if callLine == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if callLine == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if callLine == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
cls
echo __i
echo ^|---^|
echo ^|[_]^| PHONE: %callText%
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
ping localhost -n 5 >nul
if %callText% == "END" goto UsePhone
goto Calling2
I keep crashing with: . is unexpected at this time....
Really unusual.
You forgot the %% around callLine.
:Calling2
if %callLine% == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if %callLine% == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if %callLine% == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if %callLine% == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
When testing variables with if statements, the variables must be enclosed in % %

Batch: goto was unexpected at this time error

So im making a project for school and i want to goto menu if the user input is not valid
My code is this:
:menu
echo.
echo.
echo What would you like to know first?
echo.
echo.
echo 1) Salary
echo 2) What Do they do?
echo 3) Schools near me that offer game design programs
echo 4) Requirements
echo 5) Credits
echo.
echo.
set /p choice="Enter Number: "
if %choice% == 1 goto salary
if %choice% == 2 goto do
if %choice% == 3 goto schools
if %choice% == 4 goto req
if %choice% == 5 goto credits
goto menu
when i enter input, there is a line that flashes
Goto was unexpected at this time then exits
What am i doing wrong?
I should add that my input is not a valid option. (1,2,3,4,5)
if %choice% == 1 goto salary
Imagine, what this would do, when %choice% is empty. The line would then be read as:
if == 1 goto salary
Obviously a syntax error. "goto was unexpected".
To avoid this, use
if "%choice%" == "1" goto salary
This would be read as:
if "" == "1" goto salary
Syntax is ok now.

Using a userinput as a variable in an If statement in BAT

#echo off
set /p a=Enter 1,2,3:
if %a%==1
echo you entered 1
if %a%==2
echo you entered 2
if %a%==3
echo you entered 3
The conversion from a variabel that is a integer to a user input that is also an integer may require quotations but this is not the error that is preventing me from using this method in code. I've used this so many time it is embarrasing that i forgot how to do it. Thanks for help.
The entire IF statement must reside on one line
#echo off
set /p a=Enter 1,2,3:
if %a%==1 echo you entered 1
if %a%==2 echo you entered 2
if %a%==3 echo you entered 3
unless you use parentheses
#echo off
set /p a=Enter 1,2,3:
if %a%==1 (
echo you entered 1
)
if %a%==2 (
echo you entered 2
)
if %a%==3 (
echo you entered 3
)
or line continuation
#echo off
set /p a=Enter 1,2,3:
if %a%==1 ^
echo you entered 1
if %a%==2 ^
echo you entered 2
if %a%==3 ^
echo you entered 3

Resources