Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i want to make a batch file that can do math like multiplication/division and adding/subtracting. Anyone?
Cheese, the more you eat it, the less you care about how mouldy it is!

The command you want is "set /a". goto the command line and type set /? or google set /a math batch command. Or go here and check out the set command, speciffically "set /a calculations" about midway down the page that the link for the set command takes you to.
There is a lot of info good about this around so there is no point in me repeating it.

Also note that Set /A cannot handle floating point numbers and any number starting with 0 (zero) is treated as Octal so 08 & 09 will be invalid..

ill skip the next round of octaling and ask what an octal and a hexidecimal are. plz put it in the layman's terms for a noob like me.
Cheese, the more you eat it, the less you care about how mouldy it is!

i wanted to make a simple cipher making batch it says the syntax of the math is wrong can u help?
here it is:@echo off
:start
cls
set choice=
set /p choice=What is the letter?
if not '%choice%'=='' set choice=%choice:~0,2%
if '%choice%'=='1' goto z
if '%choice%'=='2' goto y
if '%choice%'=='3' goto x
if '%choice%'=='4' goto w
if '%choice%'=='5' goto v
if '%choice%'=='6' goto u
if '%choice%'=='7' goto t
if '%choice%'=='8' goto s
if '%choice%'=='9' goto r
if '%choice%'=='10' goto q
if '%choice%'=='11' goto p
if '%choice%'=='12' goto o
if '%choice%'=='13' goto n
if '%choice%'=='14' goto m
if '%choice%'=='15' goto l
if '%choice%'=='16' goto k
if '%choice%'=='17' goto j
if '%choice%'=='18' goto i
if '%choice%'=='19' goto h
if '%choice%'=='20' goto g
if '%choice%'=='21' goto f
if '%choice%'=='22' goto e
if '%choice%'=='23' goto d
if '%choice%'=='24' goto c
if '%choice%'=='25' goto b
if '%choice%'=='26' goto a
ECHO "%choice%" is not valid
goto start
:a
cls
set choice=
set /p choice=What was the previous letter?
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto A
if '%choice%'=='2' goto A
if '%choice%'=='3' goto A
if '%choice%'=='4' goto A
if '%choice%'=='5' goto A
if '%choice%'=='6' goto A
if '%choice%'=='7' goto A
if '%choice%'=='8' goto A
if '%choice%'=='9' goto A
if '%choice%'=='10' goto A
if '%choice%'=='11' goto A
if '%choice%'=='12' goto A
if '%choice%'=='13' goto A
if '%choice%'=='14' goto A
if '%choice%'=='15' goto A
if '%choice%'=='16' goto A
if '%choice%'=='17' goto A
if '%choice%'=='18' goto A
if '%choice%'=='19' goto A
if '%choice%'=='20' goto A
if '%choice%'=='21' goto A
if '%choice%'=='22' goto A
if '%choice%'=='23' goto A
if '%choice%'=='24' goto A
if '%choice%'=='25' goto A
if '%choice%'=='26' goto A
if '%choice%'=='0' goto A
ECHO "%choice%" is not valid
goto a
:A
SET /P (1*4.5)+%choice%
pauseCheese, the more you eat it, the less you care about how mouldy it is!

What are you trying to do with this "SET /P (1*4.5)+%choice%" ??
The Set /p format is SET /P Variable=(PromptString)
If you are trying to multiply 1 X 4.5 then add %choice% you must use Set /A but then I already posted that Set /A cannot handle floating point math, i.e. it will only perform arithmetic calculations on integers. So SET /A Variable=(1*4.5)+%Choice% will fail with the error message "Unbalanced Parenthesis" and SET /A Variable=1*4.5+%choice% will fail with the error message "Missing Operand"

Am I the only one who can't find the labels that all those GOTOs need?
=====================================
If at first you don't succeed, you're about average.M2

M2G - no, you're not, they jest ain't there. The only response I get to work when asked "What is the letter?" is 26, funny kinda letter but then programmers and would-be programmers are funny people!!
Valerie.

ok so i didnt check the link and i turned each leter into a number and i didn't bother to do the rest when it woulndt work. im sorry. What do i need to do to get the math problem correct and display the answer?
Cheese, the more you eat it, the less you care about how mouldy it is!

I don't at all see what the script logic is. You ask for letter input then test for a number.
"goto a
:A"Doesn't mean much because it's going to the next line regardless.
You don't have any 'math' in there. If this:
SET /P (1*4.5)+%choice%
is intended to do 'math' you need SET /A [not SET /P]. And, as stated earlier, no floating point.
=====================================
If at first you don't succeed, you're about average.M2

the point of the goto A is that if they dont type in a number that is valid, then it asks them to type in one that is. I changed it to /P but now, as Valeria rightly said, I need to balance the parenthesis of add an operator. What do theese mean? I dont know much about this stuf or else i wouldn't be asking you. i just want it do find and display the answer. What is a floating number? I tried to look it up but coulndt make sense of it. Pls help me! Your telling me what im doing wrong but not what i should do!
Cheese, the more you eat it, the less you care about how mouldy it is!

"Your telling me what im doing wrong but not what i should do!"
Right, I'll tell you what you should do.... Before you use a command, display it on the Command Prompt screen by entering the command followed by /? e.g. set/? You can then study the command and its parameters and ensure the command can produce the result you want to achieve.
"I changed it to /P but now, as Valeria rightly said, I need to balance the parenthesis of add an operator."
No - SET /P will NOT perform math in any form, you must use SET /A and the parameters must be consistent with what is permitted. Your set line could be:
Set /a variablename=(1*4)+%choice%
Because Set /A cannot perform floating point math you cannot use 4.5, in other words Set /A can only perform math on whole numbers aka integers.

Get a book [remember those?] on high school math.
Your attempt to write a 'cipher' without being conversant in math is a little like the guy trying to write a whiz-bang high end game when he can't do 'hello world' in c++
=====================================
If at first you don't succeed, you're about average.M2

soory 4 wasting ur time but when i said /p i ment /a
Cheese, the more you eat it, the less you care about how mouldy it is!

Hatlad - you're not wasting our time, we give our time voluntarily in the hope that we can help... However, we won't create your coding for you, that's your part, we will attempt to get you to correct any error.
I suggest you start with one command, say SET, read the help file and generate small scripts to test it. Here's one to start with:
:: Code start.........................
:: Batch script to display the result of multiplication and addition.
@echo off & setlocal
clsset choice=2
set output=(3*47)+%choice
echo.
echo Result is %output% ... The result should be 143
echo.
endlocal
:: Code ends...........................The code will fail to produce the correct result, go ahead & amend it to get the correct result.
If there is anything in the code you don't understand research it on the internet & come back to us only if you cannot find a reference or do not understand the explanation(s).
Another thing you can do is grab someone else's coding and go thru' it line by line so that you understand what each line does. Timo Salmi has made 160+ scripts available in the file 1CMDFAQ.TXT which can be downloaded as part of TSCMD.ZIP from http://www.usenet.com/newsgroups/co...
Some of the examples are simple four or five liners others are more complex.
Good luck

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |