#echo off
Title OutputWords
:Input
cls
echo Enter a number:
set /p input=
if %input% == "1" set /a word=One
if %input% == "2" set /a word=Two
if %input% == "3" set /a word=Three
if %input% == "4" set /a word=Four
if %input% == "5" set /a word=Five
if %input% == "6" set /a word=Six
if %input% == "7" set /a word=Seven
if %input% == "8" set /a word=Eight
if %input% == "9" set /a word=Nine
if %input% == "0" set /a word=Zero
goto Show
:Show
cls
echo Number: %word%
pause
goto Input
So how do I make it so that when I write a number, it says the number with words, why isn't this working?
I think the dilemma is with the quotes around the numbers. Batch is very finicky about quotes and spacing-- try:
if %input%==1 set /a word=One
or
if "%input%"=="1" set /a word=One
Another problem you should be having, is that when you use set /a, set expects the value you store to be an equation, which it will evaluate. Since you aren't having it do math, try:
if %input%==1 set word=One
Hope this helps.
Related
I have a project for square root calculation to get the larger side of in a right triangle using Pythagorean theorem.
Here's what I've tried :
#echo off
set /p a=Pleas Enter Value(1) =
echo.
set /p b=Pleas Enter Value(2) =
set /a c=%a%*%a%
set /a d=%b%*%b%
set /a F=%d%+%c%
echo.
:calculation Value(3)
rem root Number
√%F%
echo.
pause>nul
Batch
This is the native batch method(not accurate):
set divider=0
:loop
set /a divider=divider+1
set /a sqrt=F / divider
if %divider% equ %sqrt% goto break
if %divider% gtr %sqrt% goto break
goto loop
:break
echo %sqrt%
It will floor the number if the square root is floating point number.
Again, will not support floating point arithmetic, but this code works:
#echo off
Title SquareRoot
:StartSquareRoot
cls
echo Number:
set /p number=
call :SquareRoot %number%
echo Square: %number%
echo Root: %answer%
pause
goto StartSquareRoot
:SquareRoot
set root=1
set /a sqr=%root%*%root%
:Loop
if %sqr% LSS %number% (
set /a root=root+1
set /a sqr=root*root
goto Loop
)
(EndLocal && set answer=%root% && exit /B)
This is the new version according to your comments.
It still doesn't work.
I try to rename files like
2006 Gaspésie - Croisière à la baleine 1023.jpg
2006 Gaspésie - Croisière baleine 28.jpg
To
2006-06 Croisière baleine 001.jpg
2006-06 Croisière baleine 002.jpg
The problem is that the fields F1 and F2 are empty when I do echo and when I do rename.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set /p "NewFileName=New name file (without the extension): "
set /p "LenghtMask=Mask lenght of the number: "
set /a FileNo=0
for %%I in (*.JPG) do (
echo.
#echo off
set /a FileNo=!FileNo!+1
call :return_no_mask !FileNo! %LenghtMask% FileNoMask
#echo on
set "F1=%%~I"
echo F1 %F1%
set ExtensionName=%%~xI
set "F2=%NewFileName% %FileNoMask%%ExtensionName%"
echo F2 %F2%
rename "%F1%" "%F2%"
pause
)
goto:EOF
::--------------------------------------------------
::Return the number (fill with 0 to have the right lenght)
:: in Number to use
:: in Mask's lenght ex: 2
:: out Number
::--------------------------------------------------
:return_no_mask
set NoIn=%1
::---Search the lenght of the number
set /a LenghtNo=1
if "%NoIn%" GEQ "10" set /a LenghtNo=2
if "%NoIn%" GEQ "100" set /a LenghtNo=3
if "%NoIn%" GEQ "1000" set /a LenghtNo=4
if "%NoIn%" GEQ "10000" set /a LenghtNo=5
::---Fill with zero
set NoOut=
:test_add
if "%LenghtNo%" LSS "%2" (
set NoOut=0%NoOut%
set /a LenghtNo+=1
goto :test_add
)
set NoOut=%NoOut%%1
set %3=%NoOut%
EXIT /B 0
::--------------------------------------------------
::End of file
::--------------------------------------------------
:EOF
endlocal
Thank you
Using a batch script I would like to check if the number a user enters is a perfect square and if not find the closest number that is a perfect square.
#echo off && cls
Set /p input=
if %input% == PERFECT SQUARE echo perfect square
If %input% not == PERFECT SQUARE do (
::find closest perfect square
EDIT: I made a small mod. to get the closest perfect square, instead of the smallest.
#echo off
setlocal
cls
set /P "N=Enter a number: "
set /A "x=N/(11*1024)+40, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x=(N/x+x)>>1, x+=(N-x*x)>>31, M=x*x"
if %N% equ %M% (
echo %N% is perfect square
goto :EOF
)
set /A "I=(x+1)*(x+1), ID=I-N, MD=N-M"
if %ID% lss %MD% set M=%I%
echo The closest perfect square is %M%
#echo off && cls
setlocal enabledelayedexpansion
set /p input=
set j=0
for /l %%i in (0,1,%input%) do (
set /a test=%%i*%%i
if !test! equ %input% (
echo perfect square
goto:brk1
)
if !test! gtr %input% (
set /a delta=!test!-!input!
set /a test0=!j!*!j!
set /a delta0=!input!-!test0!
if !delta0! lss !delta! (set /a s=!j!) else (set /a s=%%i)
set /a result=!s!*!s!
echo closest perfect square: !result!
goto:brk1
)
set j=%%i
)
:brk1
#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"
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).