program falling through

December 20, 2010 at 13:31:27
Specs: Windows Vista
ok i've tried so many things to try to get this program to work but it just wont work, i can't get most of it to work.

@echo off
color f5
setlocal enabledelayedexpansion
title Navigation Window

Call :menu
if "!exit!"=="true" exit /b

echo.
echo loading FunctionList...
Call :FunctionList

echo loading ProgramList...
Call :ProgramList


cls
:menu

echo.
echo Which function would you like to use?
Set Function=
Set /p Function="Type FunctionList for a list of functions -->

if /i "%function%"=="FunctionList" (
goto :FunctionList
)

if i/ "%function%"=="Prompt" (
Start C:\Windows\System32\cmd.exe
EXIT
)

if i/ "%function%"=="ProgramList" (
Pause
goto :Programlist
pause
)

if i/ "%function%"=="DBrowse" (
Start iexplore.exe
EXIT
)

if i/ "%function%"=="Exit" (
Exit


echo "%function%" is not valid. Please try again
pause
cls
goto :menu


:FunctionList

cls
echo.
echo Prompt - New cmd prompt window
echo ProgramList - goes to a list of programs
echo DBrowse - opens default browser
echo Exit - Exits program
goto menu

exit /b


:Programlist

echo Back
echo 1. Frostwire
echo 2. Itunes
echo 3. MWP (microsoft word processor)

Set program=
Set /p program="Which program would you like to open?"

if i/ "%program%"=="Back" (
goto :Menu
)

if i/ "%program%"=="Frostwire" (
START C:\program files\Frostwire\Frostwire.exe
EXIT
)

if i/ "%program%"=="Itunes" (
START C:\Program files\iTunes\iTunes.exe
EXIT
)

if i/ "%program%"=="MWP" (
START C:\Program Files\Microsoft works\WksWP.exe

echo "%program%" is not valid. Please try again.
pause
cls


now my main problem is that the only function that works is the goto :functionlist. anything else you type in will automatically close the program. at one point i had it working so that if you typed in the wrong thing it would tell you to try again but that isnt working anymore either. just need to know if i need to include something else or change something
goto :ProgramList


See More: program falling through

Report •

#1
December 20, 2010 at 14:24:14
Well, your code is suffering from a very bad case of schizophrenia. You need to be much more consistant and disciplined in your use of subroutines versus gotos. You seem to mix them all up and have them switching identities (from subs to branches.)
Subroutines ("call :xxx") should in general always return to the point from which they were called. Subs can have gotos inside their own structure, but the only advisable goto outside their structure, and the only good way to exit the function is:
goto :eof
(Although that sounds like it's exiting the batch, it's not. If you prefer, you have the
option of putting a terminal label, such as :xit, at the very bottom of your script and
use that instead of the EOF, like:
goto :xit
(other code here)
...
:xit
::------- nothing below this point!
Branches, ("goto :xxx") need not be as careful with their structure, but they should be used with clarity, logic and consistancy if you ever want to make the program both functional ("it works") and maintainable ("it will work better...").
Forum contributors could re-write your code to make it work, but then you are not learning what you did wrong. Here's an structural outline to try and demo what I pointed out:
set flag=1
:top
call :menu
if %flag% equ 0 goto :eof
goto :top

:menu
call :functionlist
goto :eof

:functionlist
echo blah blah blah
set /p function=function:
if /i %function% equ "exit" (
set flag=0
goto :eof)
call :%function% 2>nul
if errorlevel 1 (
echo invalid choice, recycling the menu...
goto :functionlist
)
goto :eof

:prompt
echo doing function prompt
goto :eof
:programlist
echo blah blah
set /p prog=program:
call %program% 2>nul
if errorlevel 1 (
echo invalid choice, recycling the menu...
goto :programlist
)
goto :eof
:prog1
echo program one
goto :eof
:prog2
echo program two
goto :eof
::--------- end of script
i'm not a crack programmer, and i have written some very very nasty and sloppy
code in my time, but i always regret it and usually have to do a complete overhaul
when i need to change or maintain my garbage. (no reflection on your code! you just
need to work on your structural concepts and design.)
All subroutines should end with goto :eof
subroutines should never "goto" places outside their own little domain
(although they are allowed to "goto" their own label to recycle, as you can see above)
"goto :eof" will cause the program to return to the point at which the sub was called.


Report •

#2
December 20, 2010 at 14:57:36
im confused about a few things in your code structure. one, what does
call :%function% 2>nul do exactly? and why do i need it
also everything below :programlist im kind of confused with. are the subs :prog1 and :prog2 and so on where i put my different programs?
also what does recycling the menu do where the invalid choice is?

Report •

#3
December 20, 2010 at 18:03:00
Didn't we go through this the other day?

http://www.computing.net/answers/pr...


Report •

Related Solutions

#4
December 20, 2010 at 20:33:44
"im confused about a few things in your code structure. one, what does
call :%function% 2>nul do exactly? and why do i need it"

call :%function% 2>nul
if errorlevel 1 (
echo invalid choice, recycling the menu...
goto :functionlist
)
my apologies, i should have used more "standard" methods to demo.
what this code does, after user inputs some text into the var. %function%:
first, it (%function%) "expands" in the running to whatever the user enters.
if the user entered "barf", then the expanded run-time looks like:
call :barf 2>nul
If the label :barf does not exist, an error is thrown back, which can be tested
using errorlevel. The "2>nul" causes supression of the errormessage output
(channel "2") by sending it into "nul" (a black hole).
All this is just another means of achieving something like:
if "%input%"=="one" (
call :one
goto :eof
)
if "%input%"=="two" (
call :two
goto :eof
)
echo invalid input
goto :recycle_menu
::----------------
"also everything below :programlist im kind of confused with. are the subs :prog1 and :prog2 and so on where i put my different programs?
also what does recycling the menu do where the invalid choice is?"
yes, those are just "dummy placeholders" representing various actions you want
your script to undertake.
"recycling the menu" just means "try again": gives the user the same menu and
array of valid choices. In a subroutine, this typically takes the form of a "goto"
back to the subroutine's label.
If you can't make any progress, I'll be happy to help (as time permits). Always remember: "USE THE GOOGLE, LUKE!"


Report •

Ask Question