0

I am attempting to use Batch Script to write a basic trivia game to run within the DOS CMD on my laptop. It was just an experimental thing so I could practice using Batch Script some more.

The first question will execute with no issues but I can not get the second question to execute. It continues to say, "goto unexpected at this time". Is there something that I need to look at in order to fix this?

Thank you for your assistance.

Code:

@echo off
color 02
title PaleScream's Trivia Quiz
echo.
echo - - - - - - - - - - - - - - - 
echo.
echo Welcome to PaleScream's Trivia Quiz
echo.
echo - - - - - - - - - - - - - - - 
echo.
pause
cls
echo Who is that little green guy in Star Wars
echo 1 Yoda
echo 2 Darth Vader
echo 3 Luke
set /p starwars=choice1~3 
if %starwars%==1 goto Correct
if %starwars%==2 goto Incorrect
if %starwars%==3 goto Incorrect
:Correct
echo You got it right!!
pause
cls
goto continued
:Incorrect
echo Sorry you got it wrong!!
pause
goto end
:continued
echo What is the national animal of Scotland?
echo 1 Reindeer
echo 2 Unicorn
echo 3 Valais Blackneck Goat
set /p ScotlandAnimal = choice1~3 
if %ScotlandAnimal%==1 goto incorrect 
if %ScotlandAnimal%==2 goto correct
if %ScotlandAnimal%==3 goto incorrect
echo ----------------------------------
:correct
echo You got it right!!
pause 
goto continued
:incorrect
echo Sorry, try again.
pause
goto end
:continued
cls
echo you made it to the end
pause
:end

2 Answers2

1

Your fault is to one time having a trailing space with the var name
set /p ScotlandAnimal = choice1~3
and the content and then you check a var name without that trailing space
if %ScotlandAnimal%==1 goto incorrect
Since the var without space doesn't exist it evaluates to nothing, so the cmd interpreter sees
if ==1 goto incorrect
which is invalid syntax.

1

You may be better served by changing set /p to Choice and changing the structure such that you do not require multiple goto's to labels containing the same/similar output text.

@Echo Off
Color 02
Title PaleScream's Trivia Quiz

Echo(
Echo(- - - - - - - - - - - - - - - 
Echo(
Echo(Welcome to PaleScream's Trivia Quiz
Echo(
Echo(- - - - - - - - - - - - - - - 
Echo(
Timeout -1

Rem Example with correct answer as 1
ClS
Echo(   1 Yoda
Echo(   2 Darth Vader
Echo(   3 Luke
Echo(----------------------------------
Choice /C 123 /M "Who is that little green guy in Star Wars "
If ErrorLevel 2 (Call :Incorrect & GoTo :EOF) Else If Errorlevel 1 (
    Call :Correct) Else GoTo :EOF

Rem Example with correct answer as 2
ClS
Echo(   1 Reindeer
Echo(   2 Unicorn
Echo(   3 Valais Blackneck Goat
Echo(----------------------------------
Choice /C 123 /M "What is the national animal of Scotland "
If ErrorLevel 3 (Call :Incorrect & GoTo :EOF) Else If Errorlevel 2 (
    Call :Correct) Else If ErrorLevel 1 (Call :Incorrect & GoTo :EOF
) Else GoTo :EOF

Rem Example with correct answer as 3
ClS
Echo(   1 Hungry Polar Bear
Echo(   2 Starving White Shark
Echo(   3 Partner with a credit card
Echo(----------------------------------
Choice /C 123 /M "Which is the most dangerous "
If ErrorLevel 3 (Call :Correct) Else If Errorlevel 1 (
    Call :Incorrect & GoTo :EOF) Else GoTo :EOF

Rem Place rest of questions here using format as above

Rem Do not change anything below here
ClS
Echo(You made it to the end
Echo(
Echo( Press any key to exit ...
Timeout -1 1>Nul
Exit/B

:Correct
Echo(You got it right!!
Timeout 2 1>Nul
ClS
GoTo :EOF

:Incorrect
Echo(Sorry you got it wrong!!
Timeout 2 1>Nul
ClS
GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39
  • What does the EOF mean? And it it possible to have it call the same errors throughout the entire code without having to have them reprinted again and again? – PaleScream Mar 20 '17 at 18:48
  • `GoTo :EOF` is explained [here](http://stackoverflow.com/questions/37515901/where-does-goto-eof-return-to). I have already designed the code structure such that the `:Incorrect` and `:Correct` labels are only coded once, but called as required. – Compo Mar 20 '17 at 19:02
  • Thank you. I'll try it out. – PaleScream Mar 20 '17 at 19:30
  • I've updated the script to cover the `if`/`else` structure for each chosen option 1-3, hope it helps. – Compo Mar 20 '17 at 19:45