This is my script. Whenever the player goes to this part of the script it says missing operator. But it only says it the first time. Help.
:M221
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+60
echo You have earned 60 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M321
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+80
echo You have earned 80 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M421
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+150
echo You have made a spectacular sword and earned 150 gold today. Time to go home and go to bed.
pause
cls
goto home3
Not really enough information.
I'd check the contents of gold.dat.
If the file does not exist, it will give you a different response.
If it is empty ( a zero-byte file) or contains simply a space newl-line or a new-line alone, the code will work happily.
If the file contains , and a newline, you'll get the error you report.
you should post the content of "%CD%\Data Files\gold.dat", and, btw. you should remove the part %CD%\ from the path, it is not neccesarry.
That's because gold may not be a number on your first pass!
Related
I need help with an unknown issue in a recent batch file executed on Windows 10.
Proper function:
I am trying to make a math quiz which generates two numbers, a basic operand (+-x/), finds the answer, then checks it against an answer from a user. Additionally, the program is made to track certain information after a user has put in their answer. (Question number, the problem and answer, and the users response.)
My issue:
In certain scenarios the program will display the problem/answer in the "game" window rather than writing the problem/answer in the text file as intended.
The command is:
echo [Q]%m%>>"C:\Users\%username%\Desktop\%fname%.txt"
It is comment marked in the code below.
What I've done to fix the issue?
After testing for a descent while I discovered the issue seemed to revolve around the portion where it tries to move a problem/answer combo with division as the operation. This helped, but the issue started randomly popping up throughout future attempts to debug.
While I feel it is a bit frowned upon to ask two things at once I have two more issues.
Why does an operation like %a%%b%%c%=%d% (a,c,d being numbers and b being a /) break a certain command in my code consistently and how can I fix it?
Why does the same issue still randomly appear even if I erase all traces of division / which seems to cause my issue in the first place.
Here is the batch file:
::Initialization
#echo off
set /a prb=3
set file=Stat
set sss=s.
if %prb%==1 set tst=.
echo.
echo Welcome to math fire, you will get randomly generated
echo multiplication, addition, and subtraction problems. You
echo will go through a specified number before the game ends
echo and this window closes. You will be tracked in a file on
echo your desktop. You will have %prb% problem%sss%
echo.
pause
CLS
echo Get ready
timeout 5
set start=%time%
CLS
set /a num=1
set /a good=0
set /a bad=0
::===========================Set up beggining of stat file.
echo =-=-=-=-=-=>>"C:\Users\%username%\Desktop\%fname%.txt"
echo ===Start===>>"C:\Users\%username%\Desktop\%fname%.txt"
echo =-=-=-=-=-=>>"C:\Users\%username%\Desktop\%fname%.txt"
echo.>>"C:\Users\%username%\Desktop\%fname%.txt"
::===================================Main Body============
::[A] Num1 [B] Operator [C] Num2 [D] Answer [E] The problem [M] Problem and answer
:main
set /a a=%random% %% 30 + 1
::To remove the division section I changed the 4 below to 3
set /a b=%random% %% 4 + 1
set /a c=%random% %% 20 + 1
if %b%==1 set b=+
if %b%==2 set b=-
if %b%==3 set b=*
if %b%==4 set b=/
set /a d=%a%%b%%c%
set e=%a%%b%%c%
echo.
echo =============================%e%=?
echo.
timeout 1
set /p ans=+++++++++++++++++++++++++++++Answer:
set m=%e%=%d%
echo ===%num%===>>"C:\Users\%username%\Desktop\%fname%.txt"
::The following line is the glitching code
echo [Q]%m%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo [A]%ans%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo.>>"C:\Users\%username%\Desktop\%fname%.txt"
if %ans%==%d% goto p
CLS
echo.
echo -----------------------------Incorrect
set /a bad=%bad%+1
goto re
:p
CLS
echo.
echo -----------------------------Correct
set /a good=%good%+1
goto re
:re
if %num%==%prb% goto end
set /a num=%num%+1
goto main
:end
echo ===========>>"C:\Users\%username%\Desktop\%fname%.txt"
echo.>>"C:\Users\%username%\Desktop\%fname%.txt"
echo [Start]%start%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo [-End-]%time%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo [Right]%good%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo [Wrong]%bad%>>"C:\Users\%username%\Desktop\%fname%.txt"
echo.>>"C:\Users\%username%\Desktop\%fname%.txt"
echo =-=-=-=-=-=>>"C:\Users\%username%\Desktop\%fname%.txt"
echo ===-End-===>>"C:\Users\%username%\Desktop\%fname%.txt"
echo =-=-=-=-=-=>>"C:\Users\%username%\Desktop\%fname%.txt"
echo.>>"C:\Users\%username%\Desktop\%fname%.txt"
CLS
echo.
echo.
echo.
echo Congrats, check your desktop for your play info.
echo.
echo.
echo.
pause
exit
Use the syntax
>>"%filename1%" echo [Q]%m%
(my testfiles are allotted in variables)
Noting that m will contain a string such as 13/14=0
The character just before a redirector redirects the logical device number. You can get around this by moving the redirction to the start of the command or inserting a space before the redirector or using
>>"%filename1%" (
echo [Q]%m%
echo something else
echo more text
)
or
(
echo [Q]%m%
echo something else
echo more text
)>>"%filename1%"
which does the same thing - gather the output of the echoes and redirect the sum-output to the file.
Oh, BTW - batch does integer mathematics, so 13/4=3 - no decimals.
This is my first question so sorry if its a litle vague.
I have been working on a program for a wile now, it is supposed to be a schedule of tasks and the time those tasks have to be completed.
Everything works just fine but i cant figure out why this part doesn't work.
:edit
echo Select Task
echo (from 1 to 7)
set /p sel=
if %sel%=1 ( goto task1 )
And then its supposed to bring me here
:task1
pause
echo Insert Task
echo.
set /p task1=
echo.
echo Set Time
echo.
set /p %time1%=
echo.
pause
It's your IF statement. a single equals sign "=" is used as an assignment operator in batch:
set var=fubar
to compare two values use a double equals "==":
if "%var%"=="fubar" echo It'll be reet!
Hope that helps, this was one of the things that always got me when I was starting to code and you will get used to it.
As #Antonio said, there is a problem with the if statement. I also find another problem in :task1
set /p %time1%=
should be
set /p time1=
...and go to www.ss64.com/nt and take a little lesson of batch scripting.
The line: if %sel%=1 ( goto task1 ) is very incomplete;
Write it so:
if "%sel%"=="1" goto task1
I think someone asked a similar questions ,but mine is a bit different. I have this code:
#echo off
title Game
set time=1 am
timeout 10 /nobreak >nul & goto game
set time=2 am
:game
How can I make the
timeout 5 /nobreak >nul
and
goto game
work at the same time? This is how I want it to work if you still didn't get it:
The timeout starts and you play the game after the timeout ends it changes the time to 2 am. How can I do that at the same time and play the game without getting disturbed? Please help. Thanks!
No, Bob. 'tis you who doesn't get the point about the variable time. It is a reserved variable which is set by the system, but can be overridden by a user script. Virtually any other variable name, you can use - just not time, date, path, random and a few others.
As to your problem,
set "mytime=1 am"
call :starttimer
:game
... whatever
:getinput
set "response="
set /p "response=%~1"
if not exist timerfinished.txt goto :eof
:: here change "mytime"
set "mytime=2 am"
:starttimer
start /min "" timer.bat 10
goto :eof
where timer.bat is
#echo off
del timerfinished.txt 2>nul
timeout %1 /nobreak>nul
echo.>timerfinished.txt
exit
The timer.bat file simply deletes the flag-file timerfinished.txt, delays for the time set by the first parameter it receives (%1 - set to 10 in main code) then creates the file and exits.
The main code starts the timer initially using :starttimer then whenever you want to prompt-and-wait-for-a-response, you execute
call :getinput "Prompt for input "
and the response will appear in %response%.
Note that setting response to nothing initially in this routine ensures that the response is empty if the user simply presses Enter
OK - so nothing actually happens using this scheme until you enter a response, only then will the time be incremented and the game continues with an updated time. If you're expecting that the end of the timeout actually does something, really - that's not going to happen - unless you use choice to input your responses with the timeout option.
i think you are looking for a "game time", running faster than real time.
Best way: use another script to set a gametime variable (like below) in a separate (minimized) window. Whenever you need to access the current "gametime" in your main script, use <gametime.dat set /p "gametime="
GAMETIME.BAT:
#echo off
set gametime=10:00
for /f "tokens=1,2 delims=:" %%a in ("%gametime%") do (
set hours=%%a
set mins=%%b
)
:loop
call :increase_gametime
timeout 5 >nul
goto :loop
:increase_gametime
set /a mins+=10
set /a hours=hours+mins/60
set /a mins=mins%%60
set gametime=%hours%:%mins%
title %gametime%
>gametime.dat echo %hours%:%mins%
well I'm trying as title say> to make a score system. ive been googling. and tried different things but I simply cant get it to work
i want it to work like this: you type in your name. then it checks if the dll file exist in the players folder. if it doesn't, it will make it. and if it does. it will just overwrite.
if it does exist however. it will first retrieve the existing score value from the dll file.
then I want it to just add in 1 point by every time
you come to the :addscore section.
and each time it passes :addscore. this new value will then be saved to the file (replaced) and then this continues :P
heres the code I got so far: and as u can see. it does not retrieve the score from the file. and neither it will set the score value any higher than 1... any tips?
#echo off
:: User check
:usercheck
set /p usrn=Username:
set score=0
if exist "D:\General_menus\users\%usrn%\playerdb.dll" (
for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A
echo Welcome back %usrn%
goto addscore
) else (
echo Hello new player %usrn%
set score=0
)
pause>nul
:: user check end
::====================================================
::add score
:addscore
set /a score+=1
echo your score is %score%
echo saving data
goto scorecheck
::add score end
::===================================================
:scorecheck
echo %score%
echo %score% > "\General_menus\users\%usrn%\playerdb.dll"
pause>nul
goto usercheck
Problem solved by: -#Thilo
fix: remove the bunny ears from line:" for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A"
I'm sure this question has been asked, or it's obvious, but I can't seem to find the answer! I am making a sort of idle clicker game in batch, and am stuck on going to the upgrades page. The idea is that I want the player to press enter to earn money, and type "upg" to go to the upgrades page. I get an error that says the goto command was not recognized at this time. Then the script closes. Here's what I have:
#echo off
title HackerClicker 1.2.1
color 0a
:start
set /a mon = 0
echo HkrAssociation Hack protocal v. 1.2.1
echo 2015 HkrAssociation. All rights reserved.
echo.
echo press and hold enter in hack routine to auto-hack and gain money
echo.
set /p= "PRESS ANY KEY TO START ROUTINE"
goto routine
:routine
cls
echo Hack Routine v. 1.2.1
echo.
echo Balance: $%mon%
echo.
echo Type "upg" to go to upgrades
echo.
set /p cmd= "C:\users\h#ck3r\hack_routine>"
set /a mon = %mon% + 1
if %cmd%= upg GOTO upgrades
goto routine
:upgrades
echo more here later
set /p=
Thank you!
In batch, one equal sign is used for assignment, and two equal signs are used for comparison. Accordingly, you need to use two equal signs to see if %cmd% is "upg":
if %cmd%==upg GOTO upgrades