Help with code for maths script-PLEASE HELP

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Help with code for maths script-PLEASE HELP

#1 Post by Rileyh » 10 Sep 2011 00:17

Hi all,
I need my batch file to receive two numbers from the user, do +, -, *, /, and display the answers.
Then, when the sums are done, it loops to the top. Then if the user types "quit" at any stage of the batch file, it exits.

Here is my current code:
@echo off
:a
@set /p A= Enter first number :
@set /p B= Enter second number :
pause
@set C=%A% + %B% & call :answer
pause
@set C=%A% - %B% & call :answer
pause
@set C=%A% * %B% & call :answer
pause
@set C=%A% / %B% & call :answer
pause
@set C=%A% %% %B% & call :answer
pause
@set abc
if _%abc%==_"quit" goto :quit else goto :a

:answer
@set /A D=%C%
@echo %C%=%D%

:quit
exit

I can get it to do maths, but when I place in the code for the quitting part, it doesn't get past the "Enter first number" and "Enter second number part (it just flashes the black command prompt and exits)

Please help me and feel free to modify my code to give an answer, or else write a completely new script.

Thanks for your effort,
Rileyh

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Help with code for maths script-PLEASE HELP

#2 Post by Ed Dyreen » 10 Sep 2011 06:42

'
Is answer meant to be a function or procedure:

echo.0
@set C=%A% %% %B% & call :answer
echo.3
call :quit
echo.never gets here

:answer
echo.1
echo.answer
echo.2
exit /b %$error%

:quit
echo.4
exit %$error%
echo.never gets here
exit /b %$error%

You seem to have only one procedure
:answer
@set /A D=%C%
@echo %C%=%D%

:quit
exit

The way you wrote it :quit is a part of procedure :answer, this could not have been your intention.
Last edited by Ed Dyreen on 10 Sep 2011 11:01, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with code for maths script-PLEASE HELP

#3 Post by aGerman » 10 Sep 2011 09:35

EXIT is a command you normally shouldn't use in a batch code, except EXIT /B to return an ERRORLEVEL value.

Code: Select all

@echo off
:a
set "A=" &set "B="
set /p "A= Enter first number : "
set /p "B= Enter second number : "
pause
set "C=%A% + %B%" & call :answer
pause
set "C=%A% - %B%" & call :answer
pause
set "C=%A% * %B%" & call :answer
pause
set "C=%A% / %B%" & call :answer
pause
set "C=%A% %% %B%" & call :answer
pause
set "abc="
set /p "abc=Enter someting else but quit to continue: "
if /i "%abc%"=="quit" (goto :eof) else goto a

:answer
set /A D=%C%
echo %C%=%D%
goto :eof

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with code for maths script-PLEASE HELP

#4 Post by dbenham » 10 Sep 2011 15:12

aGerman wrote:EXIT is a command you normally shouldn't use in a batch code, except EXIT /B to return an ERRORLEVEL value.
Or EXIT /B to simply end a called function/subroutine :wink:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with code for maths script-PLEASE HELP

#5 Post by aGerman » 10 Sep 2011 16:45

Haha, true. Mostly I use goto :eof in this case (out of habit). But actually you could deploy exit /b to return a value to the line where the sub routine was called from.

Code: Select all

@echo off

call :sum 10 20
echo %errorlevel%

call :equals 10 20 &&echo true||echo false

pause
goto :eof

:sum
setlocal
set /a retval = %1 + %2
endlocal &exit /b %retval%

:equals
setlocal
set "retval=1"
if %1==%2 set "retval=0"
endlocal &exit /b %retval%

Regards
aGerman

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Help with code for maths script-PLEASE HELP

#6 Post by Rileyh » 11 Sep 2011 03:57

@Ed Dyreen
"Answer" is supposed to be a point of reference to the script:
":answer
set /A D=%C%
echo %C%=%D%
goto :eof"

The script "goes to :answer" for the next command and then returns to the rest of the script.

Post Reply