#echo off
:start
set CTR=1
:loop
echo Lanz
set /a ctr=%CTR%+1
if ctr LEQ 5(
echo Lanz
) else goto loop
if ctr==5 goto finish
:finish
echo %CTR%
pause
cls
goto start
I just need help on this.
The instruction is that it needs to display the name five times in a loop statement form.
It's difficult because what my teacher gave me is a flowchart, I followed everything to the letter, it's not working. Help
Why not use a for loop?
#echo off
for /l %%a in (1,1,5) do (
echo Lanz
)
pause
If you want to stick with goto, it might look similar to this:
#echo off
set ctr=1
:loop
echo Lanz
set /a ctr=%ctr%+1
if %ctr% LEQ 5 goto loop
echo %ctr%
pause
There's no need for other labels and gotos: simple as do ... while
Related
(This is my first post here, so bear with me)
Can you show the last user-input in a batch file? I'm gonna try to keep it simple here.
#echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu
I know that I could replace the (last user input) with %example%, but then I'd have to make custom error messages for every category, and there are about 50 of them. It'd be easier with a last input command.
By the way, I've taught myself everything that I know about batch, so my example probably has major issues right now, but it works somehow.
You could centralize all user input into a function (user_input)
:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu
I don't know how to do it without temp file. TO get the things written int the console you need the doskey /history (this will skip the running of the script itself):
#echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul
I am currently coding (in batch) a mini tycoon game. However,
I'm having some difficulties when executing this command:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money<"m2df.dll"
set /p cps<"m3df.dll"
if %money% GTR 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set /a "newcps=%cps%+2"
set cps=%newcps%
set money=%newmoney%
echo %money%>m2df.dll
echo %cps%>m3df.dll
echo Bought!
pause
goto shop
The program seems to exit straight after I input "1"
Any ideas how to fix this?
Update:
Found It Out. I decided to put a pause after everything to see what was wrong, and the syntax was bad and it went straight to another command crashing the script. here's the fixed script:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money=<"m2df.dll"
set /p cps=<"m3df.dll"
if %money% GEQ 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set money=%newmoney%
set /a "newcps=%cps%+2"
set cps=%newcps%
echo %cps% >m3df.dll
echo %money% >m2df.dll
echo Bought!
pause
goto shop
So i'm trying to make a small batch program with some sets and ifs, seems easy enough right? Apparently, you can't use "set" and "if" like this.
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
Then continue the code and later in the program do this.
If %variableTrue% EQU 1 goto next
Please note I've tried it with exclamation marks as well.
With the exclamation marks, it's like it completely ignores the statement, even if it's true it will continue as usual. With the percentage signs, it says very quickly before crashing
"1" was not expected at this time.
Or something like that, like I said it barely stayed for half a second.
I always thought you could do it like this as long as there are no conflicting variables.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
That doesn't give an error, it just ignores the statement and keeps going as usual.
UPDATE:
After fixing errors this still doesn't work.
When I use exclamation marks it ignores the line, and when I use percentage signs it says:
"
"1 was not expected at this time"
One set of problems is your IF statements. For example, If "!coolestNum!"== 1 goto cool. The quotes are included in the comparison.
You need to be symmetric - either include quotes on both sides
If "!coolestNum!" == "1" goto cool
or neither:
If !coolestNum! == 1 goto cool
The same problem exists with If "!fn!" EQU 13 goto setvar, as well as the line before it.
The other problem you have is set "coolestNum==1" has an extra, unwanted =. The second = becomes part of the value. You only want two equal signs with IF comparisons.
Here is corrected code:
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
But your logic is needlessly convoluted. The following produces the exact same result.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b
Use
set variableTrue=1
rather than
set variableTrue EQU 1
This batch script:
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
outputs
A
C
In batch, I want to run something 10 times and then goto continue.
How would I do this?
Here's one approach:
#echo off
setlocal enabledelayedexpansion
echo before
set i=0
:loop1
echo something 10 times
set /a i+=1
if !i!==10 goto continue
goto loop1
:continue
echo after
exit /b 0
The easiest way to do this is a for /L loop.
for /L %%A in (1,1,10) do (
rem your code here
)
goto continue
The format for a for /L loop is (start,step,end), so (1,1,10) means start at 1 and count to 10 by 1s.
Well if you were to put this code onto a batch file:
it doesn't choose a random number (or if it does it always has the same result)
it does't seem to lose or win if you choose to fight
i've been teaching myself coding and this is my first time on a forum so please go easy on me :)
here is my code:
#echo off
title template
color 0F
pause
:menu
cls
echo 1.start
echo 2.instructions
echo 3.exit
set /p answer=Type the number of your option and press enter
if %answer%==1 goto start_1
if %answer%==2 goto instructions
if %answer%==3 goto exit
:exit
echo thanks for playing
pause
exit /b
:instructions
cls
echo instructions
echo.
echo This game is case-sensitive!
echo Just have fun with it!
pause
goto menu
:start_1
set /a s1=%random% * 3 / 32768 + 1
if %s1%==1 goto prefight_1
if %s1%==2 goto prefight_2
if %s1%==3 goto prefight_3
:prefight_1
cls
echo You have discovered 3 Turtles!
echo They dont see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_1
if %answer%==2 goto run_1
:fight_1
set /a f1=%random% * 4 / 32768 + 1
if %f1%==1 goto lose_fight_1
if %f1%==2 goto win_fight_1
if %f1%==3 goto win_fight_1
if %f1%==4 goto win_fight_1
:prefight_2
cls
echo You have discovered 3 Turtles!
echo They see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_2
if %answer%==2 goto run_1
:fight_2
set /a f2=%random% * 4 / 32768 + 1
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
if %f2%==1 goto lose_fight_1
if %f2%==2 goto lose_fight_1
if %f2%==3 goto win_fight_1
if %f2%==4 goto win_fight_1
:prefight_3
cls
echo You have discovered 3 Turtles!
echo They see you!
echo They seem angry!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_3
if %answer%==2 goto run_1
:fight_3
set /a f3=%random% * 4 / 32768 + 1
if %f3%==1 goto lose_fight_1
if %f3%==2 goto lose_fight_1
if %f3%==3 goto lose_fight_1
if %f3%==4 goto win_fight_1
:lose_fight_1
cls
echo Sorry,You LOST!
echo Thank you for playing!
echo made by: JEREMY
set /p answer==(1)continue or (2)quit?
if %answer%==1 goto start_1
if %answer%==2 goto menu
pause
:run_1
cls
echo You ran away
pause
goto start_1
First off it's better for humans to read when it's "equ" instead of "==".
The set /p VARIABLE= is easier to use when clear so use echo like so:
:Jelly_Attack_1
cls
echo AH! There's a swarm of angry jellyfish!
echo Act fast!
echo.
echo You can (1) fight or (2) RUN
set /p VARIABLE=
if %VARIABLE% equ 1 goto Fight_Jelly_1
if %VARIABLE% equ 2 goto Run
if %VARIABLE% neq 1 goto TEST
The fight part should look something like this:
:Fight_Jelly_1
cls
echo The jellyfish are more annoying than harmful!
echo You have a great advantage!
pause
set /a FIGHTNO=%random%
if %FIGHTNO% gtr 4 goto Fight_Jelly_1
if %FIGHTNO% lss 1 goto Fight_Jelly_1
if %FIGHTNO% equ 1 goto FAILED
if %FIGHTNO% equ 2 goto Fight_Jelly_1_Win
if %FIGHTNO% equ 3 goto Fight_Jelly_1_Win
if %FightNO% geq 4 goto Fight_Jelly_1_Win
Then when wanting to give gold or a reward on winning do this:
:Fight_Jelly_1_Win
cls
echo The fight was easy and you found some gold!
pause
set gold=0 (if you didn't have a gold variable)
set /a gold=gold+5 (to add gold)
goto FRAMENAME
I hope I helped!
I strongly suspect that you have set a variable random. If you've done that, then the value that you set in the environment overrides the magic variable.
You can clear it by set "random="
It's normal practice to use a setlocal after your #echo off. That way, any changes you make to the envirnment are backed out when the batch terminates. Without it, any changes made will remain until they are explicitly changed again.
Personally, I prefer
set /a value=%random% %% limit + 1
to generate a value 1..limit. If for no other reason, it's easier to type.
#echo off
setlocal
rem environment changes made after here will be backed-out when the batch finishes...
....whatever....
The setlocal command sets up a 'local' environment which exists only until the batch ends or an endlocal command is encountered.
see setlocal /? from the prompt for more info.
Your batch appears to work fine once I'd added a win_fight_1 routine.
Note however that batch will barf on set /p answer==(1)continue or (2)quit? but removing one of those = will fix it.
Beyond that, just watch whether you want to go to menu or start_1.
And the
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
will be ineffective - certainly if you've used set /a f2=%random% %% 4 + 1
Fake random--
I confirmed that %random% is driven by system clock-- http://blogs.msdn.com/b/oldnewthing/archive/2010/06/17/10026183.aspx
use the clock MS to get a random number from 1 to 1000
here is one way to get milliseconds: https://answers.yahoo.com/question/index?qid=20100816021807AAz6eL3
Another possibility to introduce randomness is to start the game and then wait for user input i.e. "ready to start?" and when they start will introduce some randomness into the amount of time since cmd.exe started.