How to resolve the algorithm Menu step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Menu step by step in the BASIC programming language

Table of Contents

Problem Statement

Given a prompt and a list containing a number of strings of which one is to be selected, create a function that:

The function should reject input that is not an integer or is out of range by redisplaying the whole menu before asking again for a number. The function should return an empty string if called with an empty list. For test purposes use the following four phrases in a list: This task is fashioned after the action of the Bash select statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Menu step by step in the BASIC programming language

Source code in the basic programming language

 function sel$(choices$(), prompt$)
   if ubound(choices$) - lbound(choices$) = 0 then sel$ = ""
   ret$ = ""
   do
      for i = lbound(choices$) to ubound(choices$)
         print i; ": "; choices$(i)
      next i
      input ;prompt$, index
      if index <= ubound(choices$) and index >= lbound(choices$) then ret$ = choices$(index)
   while ret$ = ""
   sel$ = ret$
end function


 10 M$(4) = "TICK TOCK"
 20 M$(3) = "MIRROR MIRROR"
 30 M$(2) = "HUFF AND PUFF"
 40 M$(1) = "FEE FIE"
 50  GOSUB 100"MENU
 60  PRINT M$
 70  END 
 100 M$ = ""
 110  FOR M = 0 TO 1 STEP 0
 120      FOR N = 1 TO 1E9
 130          IF  LEN (M$(N)) THEN  PRINT N". "M$(N): NEXT N
 140      IF N = 1 THEN  RETURN 
 150      INPUT "ENTER A NUMBER:";N%
 160     M = N% >  = 1 AND N% < N
 170  NEXT M
 180 M$ = M$(N%)
 190  RETURN

1 rem menu
5 rem rosetta code
10 gosub 900

20 print chr$(147);chr$(14)
30 print "   Menu   "
35 print:print "Choose an incantation:":print
40 for i=1 to 5
45 print i;chr$(157);". ";op$(i,1)
50 next i:print
55 print "choose one: ";
60 get k$:if k$<"1" or k$>"5" then 60
65 k=val(k$):print chr$(147)
70 on k gosub 100,200,300,400,500
80 if k=5 then end

90 print:print "Press any key to continue."
95 get k$:if k$="" then 95
96 goto 20

100 rem fee fi
110 print op$(k,2)
115 return

200 rem huff puff
210 print op$(k,2)
215 return

300 rem mirror mirror
310 print op$(k,2)
315 return

400 rem tick tock
410 print op$(k,2)
415 return

500 rem quit
510 print op$(k,2):print "Goodbye!"
515 return

900 rem initialize
905 dim op$(10,2)
910 for a=1 to 5
915 read op$(a,1),op$(a,2)
920 next a
925 return

1000 data "Fee fi fo fum","I smell the blood of an Englishman!"
1005 data "Huff and puff","The house blew down!"
1010 data "Mirror, mirror","You seem to be the fairest of them all!"
1015 data "Tick tock","Time passes..."
1020 data "<Quit>","You decide to leave."

  

You may also check:How to resolve the algorithm Harshad or Niven series step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the AWK programming language
You may also check:How to resolve the algorithm Date manipulation step by step in the mIRC Scripting Language programming language
You may also check:How to resolve the algorithm Primes - allocate descendants to their ancestors step by step in the Raku programming language