Computer Hope

Microsoft => Microsoft DOS => Topic started by: Batchmaniac800 on December 26, 2012, 07:39:07 PM

Title: Batch Quiz error. Goto not expected?
Post by: Batchmaniac800 on December 26, 2012, 07:39:07 PM
Hey there. This is my first post on these forums and I hope I can get hope with my issue. Lets get to business. At the start of the program when you want to either select instructions or questions, it says "goto was not expected at this time." I'm pretty sure I did everything correctly according to the MS DOS commands. Any help please? I have spaced out the parts to help you read it. Here it is:

@echo off
color e

:MENU
cls
echo Welcome to the Quiz. This is a basic test of knowledge.
echo To begin, type Q then press enter.
echo It is recommended that you read the instructions.
echo For instructions, type I then press enter.
set /p=Command?
if %input%==Q goto Q1
if %input%==q goto Q1
if %input%==I goto INST
if %input%==i goto INST

:INST
cls
echo Welcome to the instructions of the Quiz.
echo During each question, there will be choices that are letter form.
echo Do not type anything besides the letter of your choice or an error will occur.
echo When you type your letter, it is not case sensitive, so you can either type it upper case or lower case.
echo If you would like stop playing anytime throught the game, just click the X at the top right corner of the window.
echo Good luck! Type M and press enter to go back to the menu.
if %input%==M goto MENU
if %input%==m goto MENU

:Q1
cls
echo What is 5 plus 5?
echo A= 55
echo B= 10
echo C= 3.141592654
set /p input=Answer?
if %input%==B goto WIN
if %input%==A goto LOSE
if %input%==C goto LOSE
if %input%==a goto LOSE
if %input%==c goto LOSE
if %input%==b goto WIN

:WIN
cls
echo Good job, you have proven your logicalness!
pause
exit

:LOSE
echo Sorry, looks like you failed!
echo To try the quiz again, type R, then hit enter.
pause
set /p input=Command?
if %input%==R goto Q1
if %input%==r goto Q1
Title: Re: Batch Quiz error. Goto not expected?
Post by: foxidrive on December 26, 2012, 07:48:25 PM
You forgot the variable name in this line

set /p=Command?

and there is no input statement in the instructions.

And when using quotes like below it handles spaces etc.
The /i part of the IF command makes the test case insensitive.

set /p "input=Command? "
if /i "%input%"=="a" echo this is A or a.
Title: Re: Batch Quiz error. Goto not expected?
Post by: Batchmaniac800 on December 26, 2012, 07:55:22 PM
Thanks, but i'm new to batch scripts and what do you mean by I forgot the variable in "set /p=Command? "
Can you show me what it would look like with a variable?  :)
Title: Re: Batch Quiz error. Goto not expected?
Post by: foxidrive on December 26, 2012, 07:57:15 PM
set /p "input= what is your command, oh master? "
Title: Re: Batch Quiz error. Goto not expected?
Post by: Batchmaniac800 on December 26, 2012, 08:00:22 PM
Oh, thanks!  :D. I fixed it up from what you said and it's working now. Thanks a lot!
Title: Re: Batch Quiz error. Goto not expected?
Post by: Salmon Trout on December 27, 2012, 09:21:37 AM
if %input%==Q goto Q1
if %input%==q goto Q1
if %input%==I goto INST
if %input%==i goto INST


You don't need to check separately for upper and lower case if you use IF with the /i switch (i for ignore case)... for example the above can be replaced by:

if /i %input%==Q goto Q1
if /i %input%==I goto INST


Also if the user just hits ENTER at the set /p prompt %input% will be empty and the script will halt with an error at the IF line which you can avoid like this

if /i "%input%"=="Q" goto Q1
if /i "%input%"=="I" goto INST


I always write my string comparison IF tests like this.