Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Syntax error on this batch file?  (Read 3842 times)

0 Members and 1 Guest are viewing this topic.

jofspades

  • Guest
Syntax error on this batch file?
« on: May 31, 2013, 06:00:04 PM »
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 A
Thank you!

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Syntax error on this batch file?
« Reply #1 on: May 31, 2013, 06:42:23 PM »
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
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.