Can you use cmd as a calculator?

February 23, 2011 at 22:41:56
Specs: Windows 7, Intell core2quad 2.4 / 4gb
Hi I saw somewhere on a website that set /a=4+2 gives you the awnser 6 but it gives me a missing operand error.
What i want to do is I want the user to input 2 numbers (set /p=) and then I want to assighn these numbers variables and then I want to do calculations with these numbers. If you can maybe halp me? Thanks


See More: Can you use cmd as a calculator?

Report •

February 23, 2011 at 22:44:14
You must give a variable name if you to use "=", omit the "=" and it will just output the number:

set /a variable=4+2
echo %variable%

Report •

February 23, 2011 at 23:43:28
Thank you very much that was helpful. Can I ask you something else? Can you implement a max? lets say I only want them to input up to 20. I dont think that I can stop them from typing more than 20 But maybe I can let an error come up and let them input the number again.

Thanks for your help

Report •

February 24, 2011 at 00:05:13
Validating set /p can be annoying...

@echo off


set /p num1=Enter the first number(0 - 20)!
set /p num2=Enter the second number0 - 20)!
if not defined num1 goto input
if not defined num2 goto input

for /f "delims=0123456789" %%a in ("%num1:"=%%num2:"=%") do goto input

set num1=%num1:"=%
if "%num1:0=%"=="" set num1=0
if 1%num1:"=% gtr 120 goto input

set num2=%num2:"=%
if "%num2:0=%"=="" set num2=0
if 1%num2:"=% gtr 120 goto input

set /a result=num1 + num2

echo %result%

Report •

Related Solutions

February 24, 2011 at 00:21:58
Luckily I only want to use it on two different numbers and not more. Thanks again for your help. Would not have found that out by myself. One more thing. Can you multiple with decimals like 1.5? Because it gives me Missing operator error if I do this. Thanks again

Report •

February 24, 2011 at 00:29:26
"set /a" is integers only, if you want to use floats you can either use another language or find/write a script to deal with floats manually.

Something along the lines of

(I didn't write the multiplier, just the floating point handling on top of it).

It's still hopelessly impractical, probably better to find a better language or at least a utility.

Report •

February 24, 2011 at 05:10:23
Ok thanks I found a way to do it in the end.
can you maybe help me to split up your previous code? Lets say that I have 4 integers and I want the first and last one to have limitations. not the other two. I cant seem to get It right to enter the first number with a limitation of 20 and then to enter the other two (any number) and then to enter the last number with a limitation again..

Thanks for all of your help so far

Report •

February 24, 2011 at 05:20:07
Dont worry I made a simple mistake and I got it now. Thanks very much for your help

but smething else that I would like to ask you is, how do I change that code if I want the input to be limited to 10 and not 20

Report •

February 24, 2011 at 05:40:07
if 1%num2:"=% gtr 120 goto input

You could jut change 120 to 110.

Report •

February 24, 2011 at 06:47:07
Thanks again for your reply.
Ive got a problem lol ive got lots. but anyway
Set /a SH_1=15
set /a SH_2= 35
set /a SH_3=45
set/a SH_4=60
set /a SH_5=75
Set /a SH_6=90
set /a SH_7=110
set /a SH_8=130
set /a SH_9=150
set /a SH_0=180
set /p Lvl= What level is your shield? (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
if (%Lvl%=='1') (
goto SH1
) else goto 2
if (%Lvl%=='2') (
goto SH2

this for one or other reason doesnt work. Can you maybe advise me in what direction to look?

Report •

February 24, 2011 at 11:21:19
if %Lvl%==1 goto SH1

if %Lvl%==2 goto SH2

Happy is ONE who says I am an OttoMAN.

Report •

February 24, 2011 at 11:26:11
Thanks a lot. I got that a while ago but still thanks. I think that im aboput finished with this project. Again thanks for all of your help.


Report •

Ask Question