Saved Bookmarks
| 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. ECHO offThe 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 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. |
|