70

This is my first time on Stack Overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine.

Here is the code I have been using:

@echo off
set a=3
set b=4
set c=%a%+%b%
echo %c%
set d=%c%+1
echo %d%

The output of that is:

3+4
3+4+1

How would I add the two variables instead of echoing that string?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
swarajd
  • 977
  • 1
  • 10
  • 18

6 Answers6

97

You need to use the property /a on the set command.

For example,

set /a "c=%a%+%b%"

This allows you to use arithmetic expressions in the set command, rather than simple concatenation.

Your code would then be:

@set a=3
@set b=4
@set /a "c=%a%+%b%"
echo %c%
@set /a "d=%c%+1"
echo %d%

and would output:

7
8
staticbeast
  • 2,073
  • 2
  • 22
  • 24
  • 3
    I tried that, and it didn't work. it output ECHO is off and ECHO is off. – swarajd May 20 '12 at 17:01
  • If you save the code into a .bat file and run it from the command line, it produces the output `7 8`. The echo command will still output if used specifically, even when echo is off. What do you see output when you run the code? – staticbeast May 20 '12 at 17:07
  • I saved the code into a .bat file. One thing though. I run linux and I ran the code from DOSbox. I ran the program by typing the name of the .bat file into DOSbox (after mounting and all that extra procedure). The output is "ECHO is off" and on the next line "ECHO is off" – swarajd May 20 '12 at 17:12
  • Ah, I'm running under windows. I've updated the code so that instead of turning echo off, it suppresses each code line individually. What does that output for you? – staticbeast May 20 '12 at 17:15
  • 1
    Well, I installed DOSBox on my Windows XP machine and replicated what you're seeing. DOSBox doesn't appear to handle arithmetic set `SET /a` and I can't find any patch information in their sourceforge. You may need to try a different dos emulator to get this to work. – staticbeast May 20 '12 at 18:23
  • thanks. Curse DOSbox! and I thought it was amazing. Again, thanks for your help, working so hard to help me out, using your own time for someone else, you keep being a good person. – swarajd May 21 '12 at 00:17
  • I met the similar issue today: at first I tried with "/a" and it didn't work; then I tried "/A" and it worked! When I switched back to "/a", it still worked... I had no ideas why this happened.. I'm using Windows 7. – yaobin May 15 '13 at 07:54
  • I discovered that you must use variables. Meaning set /a dt=%dd%-%daysBack% works but set /a c=%dd%-7 does not. hth the lurkers. – Joe Johnston Jun 25 '16 at 18:26
  • Good solution, you saved my time :) – ChikuMiku Sep 06 '16 at 21:46
  • 2
    `set /a c = %a% + %b%` gives me `Missing operand.` – johny why Sep 21 '16 at 17:41
  • 2
    @staticbeast because `set /a` is a Windows NT's cmd extension. DOS doesn't have that. [Windows cmd is **not** DOS](http://superuser.com/q/451432/241386) – phuclv Nov 14 '16 at 16:56
  • Maybe too late but I see someone is having trouble with getting output of 'ECHO is off' instead of value of the variable. Use ! charaters instead of %. For example like `echo !variable!` instead of `echo %variable%` – Yılmaz Alpaslan Jun 13 '21 at 15:19
3

According to this helpful list of operators [an operator can be thought of as a mathematical expression] found here, you can tell the batch compiler that you are manipulating variables instead of fixed numbers by using the += operator instead of the + operator.

Hope I Helped!

lolzeryest
  • 66
  • 2
1
@ECHO OFF
TITLE Addition
ECHO Type the first number you wish to add:
SET /P Num1Add=
ECHO Type the second number you want to add to the first number:
SET /P Num2Add=
ECHO.
SET /A Ans=%Num1Add%+%Num2Add%
ECHO The result is: %Ans%
ECHO.
ECHO Press any key to exit.
PAUSE>NUL
Sreenikethan I
  • 319
  • 5
  • 17
Luke
  • 19
  • 1
1

You can solve any equation including adding with this code:

@echo off

title Richie's Calculator 3.0

:main

echo Welcome to Richie's Calculator 3.0

echo Press any key to begin calculating...

pause>nul

echo Enter An Equation

echo Example: 1+1

set /p 

set /a sum=%equation%

echo.

echo The Answer Is:

echo %sum%

echo.

echo Press any key to return to the main menu

pause>nul

cls

goto main
Richie
  • 19
  • 1
  • As far as terminology goes, you are not "solving any equation" here. You are instead "evaluating an expression" involving variables with known values. Solving an arbitrary equation would require symbolic math of some kind and would be a great deal more complicated! – Some Guy May 08 '18 at 00:20
0
@ECHO OFF
ECHO Welcome to my calculator!
ECHO What is the number you want to insert to find the sum?
SET /P Num1=
ECHO What is the second number? 
SET /P Num2=
SET /A Ans=%Num1%+%Num2%
ECHO The sum is: %Ans%
PAUSE>NUL
Sreenikethan I
  • 319
  • 5
  • 17
-1

here is mine

echo Math+ 
ECHO First num:
 SET /P a= 
ECHO Second num:
 SET /P b=
 set /a s=%a%+%b% 
echo Result: %s%
  • This is nearly the same (3 years old) answer like the answer from Tamil selvan. – jeb Oct 22 '17 at 08:15