0

My problem is that I have to make a for loop that asks the user to type something 5 times and then do something depending in the user answer, my idea was to put a if else inside the for loop but that makes the program crash. Is there a easy way to make this? Also sorry if my english is not the best.

My code idea:

@echo off

for /l /f %%x in (1, 1, 5) do (

    set /p option= type option
    
    if %option%==a (
        echo option a
    ) else (
        if %option%==b (
            echo option b
        ) else (
            echo another option
        )
    )
)

pause
exit

1 Answers1

0

See code below:

Changes:
removed /f from for-loop (syntax errors)
moved if-construct to subroutine (avoids need for delayedexpansion)
changed exit to exit/b (easier to run from commandline in cmd)

Suggestions:
For easier detection of errors start with echo on.
Run the batchfile from the commandline.
Replace the set/p with the choice command.

@echo off
for /l %%x in (1, 1, 5) do (
    set /p option= type option
    call :sub
)
pause
exit/b

:sub
    if %option%==a (
        echo option a
    ) else (
        if %option%==b (
            echo option b
        ) else (
            echo another option
        )
    )
exit/b
OJBakker
  • 602
  • 1
  • 5
  • 6