-1

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
dan1st
  • 12,568
  • 8
  • 34
  • 67
  • 1
    It doesn't set %ERRORLEVEL% the way you appear to think it does. Please see [SS64 on `CHOICE`](https://ss64.com/nt/choice.html) and [SS64 on `ERRORLEVEL`](https://ss64.com/nt/errorlevel.html). – Jeff Zeitlin May 14 '21 at 18:43
  • I don't understand whats wrong though. "Accept user input to a batch file. Choice allows single key-presses to be captured from the keyboard." Shouldn't i be able to do it like i did? – Kevin Løfgren May 14 '21 at 18:50
  • `CHOICE` only sets `ERRORLEVEL` to a numeric value, not a character value. `IF ERRORLEVEL x` only tests to see if `ERRORLEVEL` is ***equal to or greater than*** x. Please read the **whole** page, not just the first part that tells you what you think you want to see. – Jeff Zeitlin May 14 '21 at 18:55
  • The help file for the `CHOICE` command is pretty `CLEAR` on what the `ERRORLEVEL` is set to. **The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on.** What don't you understand? Open up a command prompt and type: `choice /?`. – Squashman May 14 '21 at 20:09
  • I saw in the examples that there was used letters, but is there any alternative way of capturing letters instead of numerical values? – Kevin Løfgren May 14 '21 at 20:28
  • `set /p "input=Chose a menu option:"` – Squashman May 14 '21 at 23:21
  • yes but then it doesn't go instantly to the next page, i have to press enter afterwards – Kevin Løfgren May 15 '21 at 00:35

2 Answers2

1

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.
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
0

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
Squashman
  • 13,649
  • 5
  • 27
  • 36