What am i doing wrong using the Choice command with batch?

1.5k Views Asked by At

This is not the full code, since its about 1000 lines long, but here's the problem, when i come to this section of the game

choice /c abc1 /n

when i press "a" it's supposed to "medicalbag" and instead it acts like if i were to press "1" and goes back to start

when i press "b,c,1" they all go to "medicalbag".

i can't find a solution to this, i read about the command and apparently it supports these letters and numbers, when i change them out with just numbers they work just fine, but im really not sure what im doing wrong here.

:bag 
cls
echo                    *****************************
echo                    a) Medical supplies
echo                    b) Consumables
echo                    c) Weaponry
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c abc1 /n

if %errorlevel% == a goto medicalbag
if %errorlevel% == b goto consumablebag
if %errorlevel% == c goto weaponrybag
if %errorlevel% == 1 goto start

:medicalbag
cls
echo                    *****************************
echo                    Bandages: %bandagecount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c 1 /n

if %errorlevel% == 1 goto bag

:consumablebag
cls
echo                    *****************************
echo                    Canned food: %cannedfoodcount%
echo                    Purified water: %purifiedwatercount%
echo                    Dirty water: %dirtywatercount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c 1 /n

if %errorlevel% == 1 goto bag

:weaponrybag
cls
echo                    *****************************
echo                    a) combatknife: %combatknifecount%
echo                    -----------------------------
echo                    1) back
echo                    -----------------------------
choice /c a1 /n

if %errorlevel% == a goto combatknifecheck
if %errorlevel% == 1 goto bag
2

There are 2 best solutions below

1
T3RR0R On BEST ANSWER

the errorlevel of the choice keys defined using the /c switch is returned according to the keys position in the list.

with /c abc1:

keypress:   errorlevels reuturned:
a           1
b           2 
c           3
1           4

To get the literal keypress, you need to use a for /f loop to capture the keypress:

For /f "delims=" %%G in ('choice /n /c abc1')Do Echo(You pressed: %%G

  • Note - the /n switch MUST be used when using a for loop to capture the pressed key.
0
Squashman On

Here is an example of a dynamic menu system that will return the user selected value and the description of that value.

@echo off
setlocal EnableDelayedExpansion

CALL :MENU  "Medical supplies" "Consumables" "Weaponry" "Back"
CALL :CHOSE

pause
goto :eof

REM ONLY FUNCTIONS BELOW HERE
:MENU
set /a "OptionNo=0"
set "choices="
FOR %%I IN (%*) do (
    set /a OptionNo+=1
    call :ResolveChar !OptionNo!
    set "Option!retval!=%%~I"
    set "choices=!choices!!retval!"
    echo [!retval!] %%~I
)
GOTO :EOF

:ResolveChar
SETLOCAL
  set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  set "_startchar=%1"
  CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof

:CHOSE
choice /c !choices! /cs /m "Select Your Option: " /n
call :ResolveChar %errorlevel%
set "Option=!Option%retval%!"
echo You Selected: %retval% - %Option%
GOTO :EOF