batch file - How To Set Variable Value In If -




hi make simple batch file tried add correct answers number @ end keeps saying 0 because variable values not changing when answers chosen. here code below

@echo off title game 1 color 1f ::############################# :one set correctn=0 set correctn2=0 cls echo 2 + 2? echo. echo. echo a) 6 echo b) 4 echo c) 49 echo d) 17 echo. echo. echo type correct answer. set /p ch1= echo. echo. if not defined ch1 (goto one) if %ch1%==a goto no if %ch1%==a correctn=0 if %ch1%==b goto yes if %ch1%==b correctn=1 if %ch1%==c goto no if %ch1%==c correctn=0 if %ch1%==d goto no if %ch1%==d correctn=0 pause>null ::######################################### :no cls echo sorry, answer incorrect. echo. echo. echo correct choice b, 4. pause>null goto 2 ::######################################### :yes cls echo correct. congratulations. echo press key continue. pause>null goto 2 ::########################################## :two cls echo 100 divided 2? echo a) 45 echo b) 50 echo c) 90 echo d) 17 echo. echo. set/p ch2= echo. echo. if not defined ch2 (goto two) if %ch2%==a goto no2 if %ch2%==a correctn2=0 if %ch2%==b goto yes2 if %ch2%==b correctn2=1 if %ch2%==c goto no2 if %ch2%==c correctn2=0 if %ch2%==d goto no2 if %ch2%==d correctn2=0 echo invalid choice, please try again! pause>null ::################################# :no2 cls echo sorry, answer incorrect. echo. echo. echo correct choice b, 50. pause>null ::######################################## :yes2 cls echo correct. congratulations. echo press key continue. pause>null goto end ::###################################### :end set/a correct=%correctn% + %correctn2% echo number of answers correct %correct% pause>null 

so how change variable value in if statement if variable exists?

you need set variable first , goto. written, script goes label yes first. hence, if %ch1%==b set correctn=1 line never reached:

 if %ch1%==b goto yes if %ch1%==b set correctn=1 if %ch1%==b goto yes 

moreover, if command string comparison case sensitive without /i switch; try following:

if /i %ch1%==b (set correctn=1&goto yes) 

i'd suggest using (windows native) choice.exe instead of set /p single key-press user input, e.g follows:

choice /c abcd if %errorlevel%==2 (     set correctn=1     goto yes ) else (     set correctn=0     goto no ) 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -