Batch-file wrong input fix? - batch-file

#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
If /i "%INPUT%" == "" goto Wrong
I have a strange problem. Above is a code and for some reason, when I type in "INPUT" something like 'Aiden' it would think it meant '1'. Is there a way to make every wrong answer goto wrong. (Wrong answers like Aiden which isn't even specified there. But not only Aiden, any other thing).

After your if statements you need to put a catch-all GOTO statement so if none of the others work, it will go to wrong instead of just continuing into the block right below
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
goto Wrong

Related

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

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
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.
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

There is an issue with my batch file code, can't find the problem

So I'm making a very ambitious game using batch to challenge myself. I've been working on the "shop" segment of this game but for some reason when I enter 'b' or 'B' it doesn't send me back to the menu, but reads it as I wanted to buy a medkit and sends me to the "medkit purchased" line of code. I've tried to add pause >nul and pause between the shop line of code and the purchased line of code, but it didn't help. I also tried double checking to see if I typed menu right many times. There has to be something I'm missing, help appreciated, I've been messing with it for almost 2 hours now.
Edit: I have not entered the other events yet, only medkit and go back to menu.
Edit2: I've added some more variables and instead of taking me to them it takes me to the medkit code again.
Edit: I removed the percents, you guys can stop bugging me bout it. The issue still is not fixed and I do not have choice.exe so choice commands don't work.
Here is the code:
:shop
cls
echo Welcome to the shop!
echo Money: $%money%
echo.
echo 1. Buy Med Kit -$30
echo 2. Buy Shotgun Ammo (1) -$15
echo 3. Buy Assult Rifle Ammo (5) -$20
echo 4. Buy Missle (1) -$50
echo 5. View Armor
echo 6. View Modifiers
echo.
echo Enter 'B' to go back.
echo.
set /p %shopOp%=
if '%shopOp%' == '1' goto medkit1
if '%shopOp%' == '2' goto sg1
if '%shopOp%' == '3' goto ar1
if '%shopOp%' == '4' goto rpg1
if '%shopOp%' == '5' goto armors1
if '%shopOp%' == '6' goto mods1
if '%shopOp%' == 'B' goto menu
if '%shopOp%' == 'b' goto menu
:medkit1
cls
set /a money=%money%-30
if %money% LSS 0 goto noBuy1
set /a medK=%medK%+1
echo You bought 1 medkit for $30!
echo You now have %medK% med kits now!
echo.
echo Press enter to continue
pause >nul
goto shop
:noBuy1
cls
set /a money=%money%+30
echo You dont have enough money!
echo You have $%money%. You need $30.
echo.
echo Press enter to continue
pause >nul
goto shop
Okay so you don't have choice. How about xcopy?
A version adapted from the link in my comment that substitutes the choice utility with xcopy to return keypress:
#Echo off
Set Echo.ID=Call :ID.Label "%%~1" "%%~2"
Setlocal EnableDelayedExpansion
Title Menu Navigation Example
:Bar
Set "Left.Date="
IF /I "!Left.Bar!" == "Yes" Goto :Date
Call :[+Menu] "Bar" "Drink Fight Chat Leave"
Goto :Bar
:Date
Set "Left.Bar="
IF /I "!Left.Date!" == "Yes" Goto :Bar
Call :[+Menu] "Date" "Dinner Flirt Leave"
Goto :Date
:[+Menu] <Location / Core Menu option> <Sub Actions / Menu Items as list>
::: - Build menu and choice list for display
CLS
Set "CHOICES="
Set "Actions="
For %%A in (%~2) Do (
Set "Option=%%~A"
Set Actions=!Actions! "%%~A"
Set CHOICES=!CHOICES! "!Option:~0,1!"
Set "Option=[!Option:~0,1!]!Option:~1,100!"
Echo !Option!
)
::: - Return key literal from xcopy command and call relevent menu item with Identifying Params
Set "Option="
Set Key=
For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
If Not Defined Key Set "Key=%%A"
)
Set "Key=!Key:~-1!"
::: - Restrict keypress to available options, fall through to 'Goto :[Menu]' when no Call is made (Invalid keypress)
For %%O in (%~2) Do (
Set "Option=%%~O"
Set "Option=!Option:~0,1!"
If /I "!Option!" == "!Key!" Call :[%~1_%%~O-!Key!] "%%~1 %%~O" "%~1"
)
Goto :[+Menu]
:[Bar_Drink-D]
%Echo.ID%
Goto :!Loc!
:[Bar_Fight-F]
%Echo.ID%
Goto :!Loc!
:[Bar_Chat-C]
%Echo.ID%
Goto :!Loc!
:[Bar_Leave-L]
%Echo.ID%
Set "Left.Bar=Yes"
Goto :!Loc!
:[Date_Dinner-D]
%Echo.ID%
Goto :!Loc!
:[Date_Flirt-F]
%Echo.ID%
Goto :!Loc!
:[Date_Leave-L]
Set "Left.Date=Yes"
%Echo.ID%
Goto :!Loc!
:ID.Label <%~1> <%~2>
Title %~1
::: - Identify return Label
Set "Loc=%~2"
Exit /B

How To Set Variable Value In If

Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the 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, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 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, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)

Batch file RPG game error

#echo off
TITLE Zombie Warrior
setlocal enabledelayerdexpansion
:new
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
goto refresh
:refresh
set health=100
set zombiehealth=200
set zombie2health=400
goto menu
:menu
cls
echo.
echo Zombie Warrior
echo Coins = %coin%
echo.
echo 1) Play!
echo 2) Exit.
echo 3) Shop
echo.
set /p c=C:\
if "%c%" == "1" goto home
if "%c%" == "2" exit
if "%c%" == "3" goto shop
goto menu
set health=100
set zombiehealth=200
:home
cls
echo Welcome to the game!
echo -+-+-+-+-+-+-+-+-+-+-+-+-
echo Coins: %coin%
echo Level: %level%
echo.
echo 1) FIGHT!
echo 2) Quit
set /p c=C:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
if "%c%" == "2" goto menu
goto home
set health=100
set zombiehealth=200
:encounter1
cls
echo You: %health%
echo Zombies Level 1: %zombiehealth%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack1
if "%c%" == "2" goto refresh
goto encounter1
:encounter2
cls
echo You: %health%
echo Zombies Level 2: %zombie2health%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack2
if "%c%" == "2" goto refresh
goto encounter2
:attack2
set /a zombie2health-=playerdmg
set /a health-=zombiedmg
if %zombie2health% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:attack1
set /a zombiehealth-=playerdmg
set /a health-=zombiedmg
if %zombiehealth% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:win1
set /a coin+=rewards
set /a level+=1
if level == 1 set /a coin+=10
goto refresh
:lose
cls
echo You lost :(
pause
goto refresh
:shop
cls
echo What would you like to buy? (type q to quit)
echo Coins: %coin%
echo 1) Baseball bat
echo.
set /p c=C:\
if "%c%" == "1" goto bat1
if "%c%" == "q" goto menu
:bat1
if %coin% lss 30 goto nope
set /a playerdmg+=5
set /a coin-=30
goto menu
:nope
echo You don't have enough coins!
pause
goto menu
So that is my code. What I'm trying to make is basically if I am level 1 or 0, it will make me go somewhere else.
For example if I am level 0
goto encounter1
if I am level 3
goto encounter4
But I don't know how to do that. I think I'm close in this line:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
Your problem is stemming from these lines:
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
All spaces are significant in a SET statement, so your values include a leading space. Your if "%level%" == "0" ( statement expands to if " 0" == "0" (, which of course is false.
Remove the leading space from the SET statements, and your immediate problem should be solved. (I haven't evaluated your entire code base to see if there are other problems)
A better way to structure your SET statements is to enclose the entire expression in quotes. The quotes mark the end of the assignment, but are not included in the value. This prevents inadvertent trailing spaced from being included in the value.
set "playerdmg=23"
set "zombiedmg=24"
set "coin=0"
set "rewards=10"
set "level=0"

Batch file if statement not working with cmd

i have looked for an answer and have not found one.
#echo off
:a
cls
set /p %a% =
if %a% == 1 goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo dident work
pause
i set a = 1 and is said that the goto command was unexpected at this time. any help would be great
Changed
ok it has been change and a new problem occurs. now when a = 1 it always goes to c not b
#echo off
:a
cls
set /p a =
if "%a%" == "1" goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo wierd
pause
#echo off
echo.
set /p a=
echo.
if %a%==1 goto b
if not %a%==1 goto c
:b
echo worked
echo.
pause
exit
:c
echo dident work
echo.
pause
exit
change this:
set /p %a% =
to
set /p a=
and
if %a% == 1 goto b
to
if "%a%" EQU "1" goto b
#echo off&cls
set /p a=Enter a number :
if "%a%" EQU "1" goto:b
echo dident work
exit/b
:b
echo Result = 1

Resources