1.

Solve : Syntax error on this batch file??

Answer»

Okay, so I decided to make a temperature converter between Celsius and FAHRENHEIT.
I recently found out about set /a, and wanted to use the arithmetic function to GOOD use.
When I run it, it says "Missing operand", but OTHERWISE continues the code.
So something is obviously WRONG with my math. Can someone help?
Here is the code:
Code: [Select]:A
@echo off
title Temperature Converter
set /p celorfah=Enter the degrees you are converting from [C/f]:
cls
if %celorfah%==c (
set /p celtemp=Enter the temperature in Celsius:
cls
set /a celmath1=%celtemp%*9
set /a celmath2=%celmath1%/5
set /a celtofah=%celmath2%+32
echo The temperature in Celsius is %celtemp%.
echo The temperature in Fahrenheit is %celtofah%.
echo.
pause
cls
goto A
)
if %celorfah%==f (
cls
set /p fahtemp=Enter the temperature in Fahrenheit:
cls
set /a fahmath1=%fahtemp%-32
set /a fahmath2=%fahmath1%*5
set /a fahtocel=%fahmath2%/9
echo The temperature in Fahrenheit is %fahtemp%.
echo The temperature in Celsius is %fahtocel%.
echo.
pause
goto A
)
cls
echo That isn't an option!
echo.
pause
goto AThank you!because you have the veriables being changed in an 'if' statement, you need to enable delayed expansion and change the format a little.

Code: [Select]:A
@echo off
setlocal EnableDelayedExpansion
title Temperature Converter
set /p celorfah=Enter the degrees you are converting from [c/f]:
cls
if %celorfah%==c (
set /p celtemp=Enter the temperature in Celsius:
cls
set /a celmath1=!celtemp!*9
set /a celmath2=!celmath1!/5
set /a celtofah=!celmath2!+32
echo The temperature in Celsius is !celtemp!.
echo The temperature in Fahrenheit is !celtofah!.
echo.
pause
cls
goto A
)
if %celorfah%==f (
cls
set /p fahtemp=Enter the temperature in Fahrenheit:
cls
set /a fahmath1=!fahtemp!-32
set /a fahmath2=!fahmath1!*5
set /a fahtocel=!fahmath2!/9
echo The temperature in Fahrenheit is !fahtemp!.
echo The temperature in Celsius is !fahtocel!.
echo.
pause
goto A
)
cls
echo That isn't an option!
echo.
pause
goto A



Discussion

No Comment Found