1.

Solve : Having issues with "CHOICE" command?!?

Answer»

Hi guys,

I'm building a game in DOS, and i'm having some real issues when I wanted to implement the CHOICE command - never used it before, but it seemed a great way to go..

Here's the code:

echo ----- Welcome to Lands of Horayan -----
echo Press 1 to Start the Game
echo Press 2 for the Options menu
CHOICE /C 12 /N
if ERRORLEVEL == 1 goto Start
if ERRORLEVEL == 2 goto Options
:Start
echo Start Game
:Options
echo Options Menu

However, It is not executing the code correctly, upon selecting "1" or "2" it simply just prints out "Start Game" & "Options Menu" in the command window..? Why is my code not displaying what it should do correctly? Surely if i press "1" it should just print out "Start Game", for example?

Do bear in mind, this is not the actual game, it's just a part i've copied out and modified to make it easier to DETERMINE what the issue is CHOICE  is working fine. 
Batch is not a high level language with lost of  nice things.
It requires the GOTO statement to be used more often.

echo ----- Welcome to Lands of Horayan -----
echo Press 1 to Start the Game
echo Press 2 for the Options menu
CHOICE /C 12 /N
if ERRORLEVEL == 1 goto Start
if ERRORLEVEL == 2 goto Options
GOTO Finish
:Start
echo Start Game
GOTO Finish
:Options
echo Options Menu
:Finisgh
echo finish

Quote

if ERRORLEVEL == 1 goto Start

There are 2 alternative errorlevel formats you can use:

(a) Old MS_DOS IF ERRORLEVEL syntax, levels in descending order

if ERRORLEVEL 2 goto Start
if ERRORLEVEL 1 goto Options

(b) Later NT command language format, levels in any order

if %ERRORLEVEL%==2 goto Start
if %ERRORLEVEL%==1 goto Options


Salmon Trout is right. A descending order needed.
Says descending order right in the help file. Quote from: Squashman on July 13, 2015, 09:31:47 PM
Says descending order right in the help file.
Thank you.Some clarification... this must be about the 10th time I have written this...

The old (true) MS-DOS legacy way of checking for errorlevels was LIKE this

If errorlevel X do_command

This meant "if the errorlevel is equal to or greater than X, do_command". Do_command was USUALLY goto a label. This meant that if you wanted to check for more than one expected errorlevel you had to do stuff like this

some_command
if errorlevel 5 goto egg
if errorlevel 4 goto bacon
if errorlevel 3 goto cheese
echo Errorlevel was 0, 1 or 2!
goto end
:egg
echo Errorlevel was 5 or more!
goto end
:bacon
echo Errorlevel was exactly 4!
goto end
:cheese
echo Errorlevel was exactly 3!
:end
echo Finished...

The errorlevel in MS-DOS and Windows before NT was held in a byte and hence could be from 0 to 255.

In true MS-DOS, if you wanted to actually find out what errorlevel a program or command returned, you had to do something like this...

some_command
if errorlevel 255 echo 255
if errorlevel 254 echo 254
if errorlevel 253 echo 253
... (snipped 251 lines)
if errorlevel 1 echo 1
if errorlevel 0 echo 0

256 lines just to get the errorlevel. All this was very cumbersome, and in Windows NT a pseudo environment variable %errorlevel% was added, which you could test in one line e.g.:

echo %errorlevel%

Also you can use all the compare-ops like EQU NEQ LSS LEQ GTR GEQ. Probably as well to remember that NT errorlevels are held in a 32 signed integer and can be anywhere from -2147483648 to +2147483647 (that's right, you can have errorlevels below zero as well as above, so the old IF ERRORLEVEL might not always do what you expect.)

Further reading:

http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx

http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html

(I like Steve Jansen's tip about using powers of 2 for your own return codes, so you can use bitwise OR and test for multiple errors at once.)

http://www.robvanderwoude.com/errorlevel.php




Discussion

No Comment Found