1

I just started to code in CMD and came with this problem! How can you print the answers with variables plus variables? Or is this a silly mistake?

file_1.bat

Title will be the called the Basic Arithmetic

title Basic Arithmetic

The main part of my code:

set /p first= First Number:
set /p sec= Second Number:

set /a ans=first+sec

To prevent the CMD closing after the answer has printed out!

set /p z= Finish!
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Andrew
  • 49
  • 1
  • 8
  • 3
    `echo %ans%`. But there is no `set /p` or `set /a` in DOS. If you didn't get a syntax error, you are not on DOS, but on Windows `cmd`. You might be interested in [SS64](https://ss64.com/nt/). – Stephan Jun 14 '17 at 15:59
  • Wait, my friend told me this was a DOS? – Andrew Jun 14 '17 at 16:05
  • 2
    a common misinterpretation. `cmd` looks very much like `DOS` but is very different. (although they both share most of the commands and their syntax). In Win95, it was called a `DOS-Box` (and indeed was DOS). In newer versions of Windows it changed to `cmd`, but most people kept the name "Dos-Box". If you call a command with `/?` (`set /?`), in most of them you notice a line "If Command Extensions are enabled...". All after that line is not DOS compatible. – Stephan Jun 14 '17 at 16:13
  • @Stephan MS-DOS won't give errors for the commands given in the question. It will create variables with names like `/p first` and `/a ans`. – Ross Ridge Jun 14 '17 at 16:47

2 Answers2

0

There is nothing wrong with your code,
and if you'd entered these commands in an open cmd window the set /a would have echoed the result of the calculation.

> set /p first=
1
> set /p second=
3
> set /a ans=first+second
4

In a batch it doesn't - it simpy stores the result to variable ans

To get the content of the variable ans either echo %ans% or set ans:

> Echo %ans%
4

> set ans
ans=4
0

While it is not the usual OS configuration, one thing that could make your code fail is to have command extensions disabled, something that will make set /p and set /a being handled as the plain set command.

You can try including a setlocal enableextensions at the start of your batch file to ensure your working environment is configured as you need.

@echo off
    setlocal enableextensions disabledelayedexpansion

    title Basic Arithmetic

    set /p first=First Number:
    set /p sec=Second Number:

    set /a ans=first+sec

    echo The answer is %ans%

    pause
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • The `cmd /?` help screen is very clear: "_The command extensions are enabled by default_". It is never necessary to use `setlocal EnableExtensions` unless the registry was specifically modified to disable command extensions, but I don't know about a single computer with such a modification... – Aacini Jun 14 '17 at 23:31
  • @Aacini, I've seen it [before](https://stackoverflow.com/q/30061623/2861476). I agree with you, the default configuration is `enabled`, but this is the easiest way to explain the `/p first` variable name, unless the OP is running NT where `set /p` did not exist. – MC ND Jun 15 '17 at 05:46
  • Where **the OP** said that there is a variable called `/p first`? That was a comment from @RossRidge about what MS-DOS do in this case. – Aacini Jun 15 '17 at 14:46