1.

Solve : Error??

Answer»

Hi, I wanted to see my hand at scripting and got started, it took me 5 minutes to make a canculator that worked, but I wanted something more... special. But one I finished coding, it didnt work! it errors.
QUOTE

ECHO off
:start
echo Welcome to Batch Canculator!
echo What do you wish number one too be?
set /p num1=
echo The number you picked was %num1%
echo What do you wish number two too be?
set /p num2=
echo The number you picked was %num2%
echo Number 3 is not needed, put a "nil" if you don't need a third number, OTHERWISE, enter the number!
set /p num3=
echo The number you picked was %num3%
echo Which would you like, to Add, Subtract, Multiply, or Divide? UNLESS you want to Leave.
set /p type=
if %type%==Add goto Add
if %type%==Subtract goto Subtract
if %type%==Multiply goto Multiply
if %type%==Divide goto Divide
if %type%==Leave goto Leave
:Add
set /Add Answer=%num1%+%num2+num3%
echo %Answer%
pause
goto start
:Subtract
set /Subtract Answer=%num1%-%num2-num3%
echo %Answer%
pause
goto start
:Multiply
set /Multiply Answer=%num1%*%num2*num3%
echo %Answer%
pause
goto start
:Divide
set /Divide Answer=%num1%/%num2/num3%
echo %Answer%
echo
pause
goto start
:Done
echo. Done!
The set command to do maths is set /a

Type set /? and you can read the portions about the arithmetic. Quote from: TheLastAlly on August 16, 2014, 05:03:34 PM
But one I finished coding, it didnt work! it errors.

You invented a set of switches for the SET command that do not exist... /Add, /Subtract, /Multiply, and /Divide. The way to produce code that works is not to guess and hope the computer understands what you meant, but rather to study the documentation.

You may be interested to know that set /a can evaluate a string e.g. 5+226 or 34-12 or 27*5 or 55/11 or (5*30)/10 or 6+5+4+3+2+1

e.g.

Code: [Select]echo off
echo Welcome to Batch Calculator!
:loop
set /p tocalc="Enter an expression, or 0 to quit: "
if "%tocalc%"=="0" goto done
set /a result=%tocalc%
echo Answer=%result%
goto loop
:done
echo Done!
sample run:

(Do you notice anything about the 4th result?)

Code: [Select]Welcome to Batch Calculator!
Enter an expression, or 0 to quit: 5-3
Answer=2
Enter an expression, or 0 to quit: 6+5+4+3+2+1
Answer=21
Enter an expression, or 0 to quit: 15*3
Answer=45
Enter an expression, or 0 to quit: 10/3
Answer=3
Enter an expression, or 0 to quit: 0
Done!Isn't 10/3 like 3.33333333333333
Or unless it doesn't go into decimals
Quote from: shiverbob on August 17, 2014, 12:34:10 AM
Isn't 10/3 like 3.33333333333333
Or unless it doesn't go into decimals

You got it.

Now try 10000000000/2

Quote from: Salmon Trout on August 17, 2014, 12:24:48 AM
The way to produce code that works is not to guess and hope the computer understands what you meant, but rather to study the documentation.

Of course that is not right; making mistakes and learning from them is part of learning too.


Discussion

No Comment Found