Creating+a+Menu+Driven+Program

CREATING A MENU DRIVEN PROGRAM
An outline for a typical student assignment.

We often see requests for help with a class assignment that requires a Menu of choices and some routines to Add and Display data, and to Quit the program. Usually, these programs require the user to input a list of data when the program runs, but not to save the data when the program ends. The user is required to re-enter the data each time he runs the program.

This simple program attempts to present a solution for this type of assignment. It is generic and limited in functionality. The goal is to provide a workable program which a new coder can then expand to suit the needs of his class assignment.

LISTING #1 (The bare bones) -
code format="lb" [menu] cls print "1 -- Add a record" print "2 -- List a record" print "3 -- Quit" print input "Choose a number: "; choice print if choice = 1 then goto [addData] if choice = 2 then goto [listData] if choice = 3 then goto [quit] wait

[addData] input "Enter your data (your name, for example): "; stuff$ print input "Press enter to return to menu -- "; dummy$ if dummy$ = "" then goto [menu] wait

[listData] print "LISTING OF YOUR DATA" print stuff$ print input "Press enter to return to menu -- "; dummy$ if dummy$ = "" then goto [menu] wait

[quit] print "End of program" end code

Entering only one piece of data DOES NOT A USEFUL PROGRAM MAKE! The next example allows the user to input up to 10 items, and then to list them. Still, all 10 items have to be entered at one time, and they are lost when the program ends. There is no provision for entering fewer than 10 items in this example without the program mal-functioning or crashing.

LISTING #2 (Improved version) --
code format="lb" dim myData$(10) 'dimension array for 10 data items [menu] cls print "1 -- Add a record" print "2 -- List a record" print "3 -- Quit" print input "Choose a number: "; choice print if choice = 1 then goto [addData] if choice = 2 then goto [listData] if choice = 3 then goto [quit] wait

[addData] For x = 1 to 10 Input "Enter data (a name, for example): ";myData$(x) Next x input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu] wait

[listData] print "LISTING OF YOUR DATA" print For x = 1 to 10 print myData$(x) Next x print input "Press enter to return to menu -- "; dummy$ if dummy$ = "" then goto [menu] wait

[quit] print "End of program" end code

It would be nice to enter only one data item at a time and let the program tell us when the limit of 10 items has been reached. This change to the [addData] branch will do that.

PARTIAL LISTING #3 -
code format="lb" [addData] maxRecords = 10 if count >= maxRecords then print "All spaces filled." else count = count + 1 Input "Enter data (a name, for example): ";myData$(count) end if input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu]

'...and change the [listData] branch to display only what we have so far: [listData] print "LISTING OF YOUR DATA" print For x = 1 to count print myData$(count) Next x print input "Press enter to return to menu -- "; dummy$ if dummy$ = "" then goto [menu] wait code

Some error trapping should be added so things work as expected. First, make sure the menu selection is valid.

PARTIAL LISTING #4 --
code format="lb" [menu] cls print "1 -- Add a record" print "2 -- List a record" print "3 -- Quit" print input "Choose a number: "; choice print if choice = 1 then goto [addData] if choice = 2 then goto [listData] if choice = 3 then goto [quit] 'if choice is not in range, do this... print "Out of range" input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu] wait

'Then, do something if user hits enter before typing any data. The 'computer will think the blank space is just another piece of data, 'and will add it to the list. We need to be sure it isn't added to 'the count and displayed as a blank line when [listData] is called.

[addData] maxRecords = 10 if count >= maxRecords then print "All spaces filled." else count = count + 1 Input "Enter data (a name, for example): ";myData$(count) if myData$(count) = "" then print "Invalid data input." count = count - 1 end if end if input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu] wait code

Putting the modifications together, we have a workable program that catches common user errors.

LISTING #5 (FINAL) ---
code format="lb" dim myData$(10) [menu] cls print "1 -- Add a record" print "2 -- List a record" print "3 -- Quit" print input "Choose a number: "; choice print if choice = 1 then goto [addData] if choice = 2 then goto [listData] if choice = 3 then goto [quit] 'if choice is not in range, do this... print "Out of range" input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu] wait

[addData] maxRecords = 10 if count >= maxRecords then print "All spaces filled." else count = count + 1 Input "Enter data (a name, for example): ";myData$(count) if myData$(count) = "" then print "Invalid data input." count = count - 1 end if end if input "Press enter to return to menu -- ";dummy$ if dummy$ = "" then goto [menu] wait

[listData] print "LISTING OF YOUR DATA" print For x = 1 to count print myData$(x) Next x print input "Press enter to return to menu -- "; dummy$ if dummy$ = "" then goto [menu] wait

[quit] print "End of program" end code