What error is my batch script getting and why?

I'm trying to make a batch script for some friends to study basic french from (just a few easy questions) but for some reason when it gets to a section at the end it closes the tab very quickly, before i can read what it says. I have a pause at the end of the script, so I don't know why it would close without performing the pause first.
@echo off
setlocal enabledelayedexpansion
title French Quiz
:start
echo French Study Quiz
echo  --------------
echo (if a question looks unfinished, the answer is D for testing reasons)
pause
goto Q1

:Q1
cls
echo Question 1: What does "COI" mean?
echo ----------------------
echo A. A type of fish
echo B. Compl‚ments d'Objet Indirect
echo C. Clicks On Impact
echo D. Connosieur d'Oranges Imparfait
set/p input1=
if %input1% EQU B goto correct1
if %input1% EQU b goto correct1
goto wrong1

:Correct1
set /a score=%score%+1
goto Q2

:wrong1
set /p q1wrong=1

:Q2
cls
echo Question 2: In Pass‚ Compos‚, what is the past participle of "manger"?
echo ----------------------------------------------------------
echo A. Manger
echo B. Mangeons
echo C. Mangera
echo D. Mang‚
set/p input2=
if %input2% EQU D goto correct2
if %input2% EQU d goto correct2
goto wrong2

:Correct2
set /a score=%score%+1
goto Q3

:wrong2
set /p q2wrong=1
goto q3

:Q3
cls
echo Question 3: If a verb is conjugated with "avoir" in pass‚ compos‚, do you accord it with the pronoun?
echo ----------------------------------------------------------
echo A. No
echo B. Yes
echo C. Sometimes
set/p input3=
if %input3% EQU A goto correct3
if %input3% EQU a goto correct3
goto Q4

:correct3
set /a score=%score%+1
goto Q4

:Q4
cls
echo Question 4:
echo ----------------------------------------------------------
echo A.
echo B.
echo C.
echo D.
set/p input4=
if %input4% EQU D goto correct4
if %input4% EQU d goto correct4
goto Q5

:correct4
set /a score=%score%+1
goto Q5

:Q5
cls
echo Question 5:
echo ----------------------------------------------------------
echo A.
echo B.
echo C.
echo D.
set/p input5=
if %input5% EQU D goto correct5
if %input5% EQU d goto correct5
goto end

:correct5
set /a score=%score%+1
goto end

:end
cls
echo Quiz finished!
pause
if %score% EQU 1 echo you scored 1 out of 5!
if %score% EQU 2 echo you scored 2 out of 5!
if %score% EQU 3 echo you scored 3 out of 5!
if %score% EQU 4 echo you scored 4 out of 5!
if %score% EQU 5 echo you scored 5 out of 5!
if %score% EQU 0 echo you scored 0 out of 5.
if %score% EQU 0 echo Incorrect Questions:
if %score% EQU 1 echo Incorrect Questions:
if %score% EQU 2 echo Incorrect Questions:
if %score% EQU 3 echo Incorrect Questions:
if %score% EQU 4 echo Incorrect Questions:
if %q1wrong% EQU 1 echo Question 1 (correct answer was B)
if %q2wrong% EQU 2 echo Question 2 (correct answer was D)
if %q3wrong% EQU 3 echo Question 3 (correct answer was A)
if %q4wrong% EQU 4 echo Question 4 (correct answer was _)
if %q5wrong% EQU 5 echo Question 5 (correct answer was _)

Open in new window

Chris MullinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
I think you want to move the PAUSE to after the score is displayed, like below.  

Also,

   set /a score=%score%+1

could be replaced by:

   set /a score+=1

I think your IF's at the end may need work too, review those to make sure they are doing what you want, it doesn't seem like they would be...

echo Quiz finished!
if %score% EQU 1 echo you scored 1 out of 5!
if %score% EQU 2 echo you scored 2 out of 5!
if %score% EQU 3 echo you scored 3 out of 5!
if %score% EQU 4 echo you scored 4 out of 5!
if %score% EQU 5 echo you scored 5 out of 5!
if %score% EQU 0 echo you scored 0 out of 5.
if %score% EQU 0 echo Incorrect Questions:
if %score% EQU 1 echo Incorrect Questions:
if %score% EQU 2 echo Incorrect Questions:
if %score% EQU 3 echo Incorrect Questions:
if %score% EQU 4 echo Incorrect Questions:
if %q1wrong% EQU 1 echo Question 1 (correct answer was B)
if %q2wrong% EQU 2 echo Question 2 (correct answer was D)
if %q3wrong% EQU 3 echo Question 3 (correct answer was A)
if %q4wrong% EQU 4 echo Question 4 (correct answer was _)
if %q5wrong% EQU 5 echo Question 5 (correct answer was _)
pause

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
You can also do case insensitive IF comparisons, as below.  Also, always enclose variable tests in some other character, like single or double quotes, to handle the case where they don't enter anything but just hit <ENTER> key.

   if %input1% EQU B goto correct1
    if %input1% EQU B goto correct1


can be replaced by this single line:

   if /i "%input1%" EQU "B" goto correct1


»bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
I think you want the wrong SET's to be like this also, rather than with the /P.

   set q1wrong=1


»bp
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Bill PrewIT / Software Engineering ConsultantCommented:
And the final results display could be a little shorter, for example.

:end
cls
echo Quiz finished!
echo you scored %score% out of 5!
if %score% LSS 5 echo Incorrect Questions:
if %q1wrong% EQU 1 echo Question 1 (correct answer was B)
if %q2wrong% EQU 2 echo Question 2 (correct answer was D)
if %q3wrong% EQU 3 echo Question 3 (correct answer was A)
if %q4wrong% EQU 4 echo Question 4 (correct answer was _)
if %q5wrong% EQU 5 echo Question 5 (correct answer was _)
pause

Open in new window


»bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
Sorry for the  individual feedbacks, wanted to share some suggestions, but not rewrite too much on you so that you could learn more in the process.  Hope it helped.


»bp
0
NVITEnd-user supportCommented:
Not sure if this affects anything but, your original code seems to have several errors:
set/p
should be:
set /p
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Another improvement: you can compress
set/p input1=
if %input1% EQU B goto correct1
if %input1% EQU b goto correct1
goto wrong1

:Correct1
set /a score=%score%+1
goto Q2

:wrong1
set /p q1wrong=1

Open in new window

to
set /p input1=
if /I %input1% EQU B (set /a score += 1) else (set q1wrong=1)

Open in new window

and similar for the other questions, which spares a lot of unnecessary code and lables ;-).
0
oBdACommented:
If you need to investigate an error in a batch script, open a command prompt, "cd" into the script folder, and start it from there, not by double-clicking it in Explorer.
That said: don't get me wrong, I really like Batch, but if you want to invest time learning a scripting language, go with PowerShell. Really.
It might appear a bit intimidating at first, but it's pretty fast to learn, and the web is full of examples.
To get you interested, here's your quiz as a PowerShell script, with some eye candy added, and the questions defined as XML. You can add new questions simply by adding new nodes like the existing ones.
Save it as Whatever.ps1, and in Unicode (see the Encoding box in Notepad in the "Save As" dialog).
One of your first commands might have to be
Set-ExecutionPolicy RemoteSigned -Force
Run it in a regular PowerShell console, not in the ISE; because of the eye candy, it requires some methods that the ISE doesn't provide.
## Save this file as Unicode (UCS-2) (Little Endian) - see the Encoding box in Notepad in the "Save As" dialog, for example!
## Define the questions here; they could be moved to a separate xml file as well.
## The @" in the next line will start a "Here-String", which is one single string that will only end when "@ is found at the very beginning of a line.
## The [xml] in front will turn the Here-String into an XML object.
$Questions = [xml]@"
<Questions>		<!-- XML Node names and Attributes are CaSe SeNsItIvE! -->
	<Question>
		<Prompt>What does "COI" mean?</Prompt>
		<Answer>A type of fish</Answer>
		<Answer Correct="true">Compléments d'Objet Indirect</Answer>
		<Answer>Clicks On Impact</Answer>
		<Answer>Connoiseur d'Oranges Imparfait</Answer>
	</Question>
	<Question>
		<Prompt>In Passé Composé, what is the past participle of "manger"?</Prompt>
		<Answer>Manger</Answer>
		<Answer>Mangeons</Answer>
		<Answer>Mangera</Answer>
		<Answer Correct="true">Mangé</Answer>
	</Question>
	<Question>
		<Prompt>If a verb is conjugated with "avoir" in passé composé, do you accord it with the pronoun?</Prompt>
		<Answer Correct="true">No</Answer>
		<Answer>Yes</Answer>
		<Answer>Sometimes</Answer>
	</Question>
</Questions>
"@

####################################################################################################
If ($Host.Name -ne "ConsoleHost") {
	Throw "This script MUST be started in a regular PS Console, not in the ISE. Sorry about that."
}

$OldTitle = [Console]::Title
[Console]::Title = "French Quiz"
Clear-Host

## Another "Here-String" to greet the user; it will be directly piped to Write-Host
@"
French Study Quiz
=================


"@ | Write-Host

$Count = $Score = 0
## $Questions is an XML object; the SelectNodes() method allows for XPath queries, and we're interested in all nodes called "Question" under "Questions"
$QuestionsTotal = @($Questions.SelectNodes('Questions/Question')).Count
ForEach ($Question In $Questions.SelectNodes('Questions/Question')) {	## Process each Question node in a loop
	$Count += 1
	## You can use variables directly in a string, if the string is defined with double quotes; $Question.Prompt is the inner text of the current XML Prompt node:
	$Message = "Question $($Count)/$($QuestionsTotal): $($Question.Prompt)"
	Write-Host
	Write-Host $Message -ForegroundColor 'White'
	## Strings can be "multiplied", too (simply X repetitions of the string that is multiplied):
	Write-Host ('-' * $Message.Length) -ForegroundColor 'White'
	$AnswerASCII = 64	## For the menu's answer letter; ASCII 65 is capital A.
	## Remember the cursor location, so that we can color the results after the user selection
	$Top = [Console]::CursorTop
	ForEach ($Answer In $Question.SelectNodes('Answer')) {	## Process all Answer nodes of a question
		$AnswerASCII += 1
		## $Answer.Correct is the "Correct" attribute of the Answer node; if it's true, save the current ASCII value
		If ($Answer.Correct -eq 'true') {$CorrectASCII = $AnswerASCII}
		Write-Host "$([char]$AnswerASCII). $($Answer.InnerText)"
	}
	Do {
		$ResponseASCII = [Console]::ReadKey('NoEcho').Key.value__
	} Until ((65 -le $ResponseASCII) -and ($ResponseASCII -le $AnswerASCII))
	If ($ResponseASCII -eq $CorrectASCII) {$Score += 1}
	$AnswerASCII = 64
	## Hop back to were the questions started, and print them again, but this time in color:
	[Console]::CursorTop = $Top
	ForEach ($Answer In $Question.SelectNodes('Answer')) {
		$AnswerASCII += 1
		$Color = [Console]::ForegroundColor
		If ($AnswerASCII -eq $CorrectASCII) {	## This is the correct answer; paint it green
			$Color = 'Green'
		} ElseIf ($AnswerASCII -eq $ResponseASCII) {	## This is not the correct answer, but the one the user picked; paint it red
			$Color = 'Red'
		}
		Write-Host "$([char]$AnswerASCII). $($Answer.InnerText)" -ForegroundColor $Color
	}
}

Write-Host
Write-Host "You scored $($Score) out of $($QuestionsTotal)!"
Write-Host "Quiz finished!"
Write-Host
[Console]::Title = $OldTitle

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.