Calculator code wont work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

Calculator code wont work

#1 Post by BatMaster » 03 Jul 2011 11:18

this is the code

Code: Select all

@echo off
SET INPUT=
ECHO Make a choice
SET /P n1=1st number:
set /p sym=symbol(+*/-):
set /p n1=2nd number:
set /a sum=%1n% %sym% %2n%
echo %sum%
pause

any help would be greatly appreciated
regards BatMaster

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Calculator code wont work

#2 Post by Cleptography » 03 Jul 2011 12:01

Oh hell why not, welcome to cmd. Welcome to batch scripting BatMaster.
If you are going to make a basic calculator as you have shown in your post. You have a few different options. You can have it as is and pause and set each piece of data as you have done. You can turn the whole thing into a utility and pass it parameters via the command line using the %1-%9 method, or you could set /p all your data to one string and parse it via the for command.

The first thing you have made a mistake with is the use of (SET INPUT=) You are not setting the input to anything therefore it is not needed. If you want to set a variable that cleans itself after use you can use (setlocal) at the top of your script.

Here is an example to help you with coding your calculator, and as most will tell you calculations in cmd are rather useless because of the limitations in cmd itself. It would be better to learn another language that is capable of performing advanced calculations using easier methods, but I imagine you are just learning, in which case more power to you, and may your adventures be fruitful.

Example 1 (Using your method)

Code: Select all

@echo off
@setlocal EnableDelayedExpansion

ECHO;Make a choice
ECHO;Enter the first numerical value.
set /p 1n=

ECHO;Enter your operator
ECHO;+ * / -
set /p sym=

ECHO;Enter the second numerical value
set /p 2n=

set /a sum=!1n! %sym% !2n!
ECHO;%sum%
pause

NOTE:
I changed your echos to separate lines though you can do it the way you have to me this just looks cleaner. I also left your variables as is because by consequence you have shown an example of why we use delayedexpansion. If you do something like %1% cmd is looking for command parameter 1 from the command line rather than the variable you are trying to pass to it via your script, if that makes sense.
In your example your variables are backwards and are not displayed as you try to set them.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Calculator code wont work

#3 Post by nitt » 03 Jul 2011 12:48

BatMaster wrote:this is the code

Code: Select all

@echo off
SET INPUT=
ECHO Make a choice
SET /P n1=1st number:
set /p sym=symbol(+*/-):
set /p n1=2nd number:
set /a sum=%1n% %sym% %2n%
echo %sum%
pause

any help would be greatly appreciated
regards BatMaster


"/a" will do equations. So if you just have

Code: Select all

@echo off
:start
set /p equ=
set /a ans=%equ%
echo %ans%
pause > nul
cls
goto start


That should work just fine.

But if you still want your method refer to the post above.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Calculator code wont work

#4 Post by trebor68 » 05 Jul 2011 00:38

In the code of BatMaster is definated only the variable "n1" for the input.
But in the row with the calculation are the variable "1n" and "2n" that not definated.

Here the correct code:

Code: Select all

@echo off
SET INPUT=
ECHO Make a choice
SET /P n1=1st number:
set /p sym=symbol(+*/-):
set /p n2=2nd number:
set /a sum=%n1% %sym% %n2%
echo %sum%
pause

Post Reply