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
Help with code for maths script-PLEASE HELP
Moderator: DosItHelp
Re: Help with code for maths script-PLEASE HELP
'
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.
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.
Re: Help with code for maths script-PLEASE HELP
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
Re: Help with code for maths script-PLEASE HELP
Or EXIT /B to simply end a called function/subroutineaGerman wrote:EXIT is a command you normally shouldn't use in a batch code, except EXIT /B to return an ERRORLEVEL value.
Re: Help with code for maths script-PLEASE HELP
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.
Regards
aGerman
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
Re: Help with code for maths script-PLEASE HELP
@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.
"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.