Saved Bookmarks
| 1. |
Solve : Division in batch files?? |
|
Answer» Hello everyone. That should be correct. Thanks for the suggestion, removing the SPACES didn't change anything though. I don't get an error message but it doesn't seem to do anything either. I just get whatever is already in the variable and the division line doesn't seem to AFFECT it.What are %num1% and %num2% They should be something along the lines of: set num1=10 set num2=2You can have as many spaces as you like. Code: [Select]@echo off set num1=15 set num2=3 set /a sum1=%num1% / %num2% echo num1=%num1% echo num2=%num2% echo sum1=%sum1% echo %num1%/%num2%=%sum1% Output: Code: [Select]num1=15 num2=3 sum1=5 15/3=5I tried your example and it worked perfectly. I played with mine and the results seem inconsistent, I'll get one answer the first time and the correct answer the second time. It seems to have something to do with being in an IF statement, if that makes any sense. This is my file, with a couple extra lines for testing purposes: Code: [Select]@Echo off rem ==== Accept user input ======= Set /p num1=Enter num1: Set /P num2=Enter num2: rem ===== Calculate and display ===== set /a sum1=%num1%+%num2% Echo %num1% + %num2% is: %sum1% set /a sum2=%num1% - %num2% Echo %num1% - %num2% is: %sum2% set /a sum3=%num1% * %num2% Echo %num1% * %num2% is: %sum3% set /a sum4=%num1% / %num2% echo %Num1% / %num2% is: %sum4% rem ==== Check for division by zero ===== if %num2% == 0 ( echo Number not valid, 0 entered ) else ( set /a sum5=%num1% / %num2% echo %Num1% / %num2% is: %sum5% ) The last line is the goofy one. The first time I run it, it looks like this: Code: [Select]Enter num1: 8 Enter num2:2 8 + 2 is: 10 8 - 2 is: 6 8 * 2 is: 16 8 / 2 is: 4 8 / 2 is: 3 The second time I run the same thing and it looks like this: Code: [Select]Enter num1: 8 Enter num2:2 8 + 2 is: 10 8 - 2 is: 6 8 * 2 is: 16 8 / 2 is: 4 8 / 2 is: 4In a parenthetical statement, such as a FOR loop or an if statement like this, any variables that are set inside the parentheses, in this case, sum5, will expand to a blank or a previous value if there is one. Code: [Select]if %num2% == 0 ( echo Number not valid, 0 entered ) else ( set /a sum5=%num1% / %num2% echo %Num1% / %num2% is: %sum5% ) you have to use delayed expansion. This is because of the way cmd.exe expands the variables at run time. You have to use this statement (put it at the START just after the @echo off line) Code: [Select]setlocal enabledelayedexpansion and in the parentheses use exclamation marks (exclamation points if you are American) like this num1 and num2 are OK because they were set before the parentheses but sum5 needs delayed expansion. Code: [Select]if %num2% == 0 ( echo Number not valid, 0 entered ) else ( set /a sum5=%num1% / %num2% echo %Num1% / %num2% is: !sum5! ) Once you are out of the parentheses you can use % signs again for sum5. Watch out for spaces in IF COMPARISONS. They can catch you out. This is better Code: [Select]if %num2%==0 This is better still Code: [Select]if "%num2%"=="0"It works, thank you so much! I added Code: [Select]setlocal enabledelayedexpansionat the top and used exclamation marks and it works! |
|