Qbasicnews.com

Full Version: GOSUB help SOLVED
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i'm working on a text rpg right now for fun, my 1st real project. anyways everything is based on menus, my problem is if i start running menus as routes from oterh menus, when i go back to the main menu, i acnt access an alrdy accessed menu, wats wrong, even just usin the input command on 1 menu selection makes the whole menu unaccessable from the main menu. can anyone maybe help me out??? or even supply a way to go from 1 menu to 4 others and still allow functions like input to work properly???
It's hard to imagine what your problem is from your description. Could you post some code?
*****
WHILE choice$ < "a" OR choice$ > "e"
CLS
PRINT "What would you like do do next?"
PRINT ""
PRINT "a. Stats"
PRINT "b. Inventory"
PRINT "c. Town"
PRINT "d. Battle"
PRINT "e. Quit"
INPUT choice$
IF choice$ = "a" THEN GOSUB Stats
IF choice$ = "a" THEN GOSUB Inventory
IF choice$ = "a" THEN GOSUB Town
IF choice$ = "a" THEN GOSUB Battle
IF choice$ = "a" THEN GOSUB Quit
choice$ = ""
Wend

That's the main menu, then the Town, Battle and Quit menu are pretty same thing just different variable and different text.

Problem: I run program, type "c", then press Enter, i select the Inn option from the Town sub menu, then when I'm done, i exit that menu, it returns me to main menu and then i can't select the Town menu, it freezes up or keeps reseting the main menu
The problem is here:

WHILE choice$ < "a" OR choice$ > "e"

This should be

WHILE choice$ < "a" AND choice$ > "e"


What you want to accomplish is that the loop should exit when choice$ is a, b, c, d or e right?

The way you have it right now is at the end of the loop, before the wend, when you set choice$ to "" it's < "a" yes (so that part is true) however it's not bigger than "e" so it exits the loop because of that second part. with an OR...but with an AND it should work as expected.
not sure y u chnged OR to AND, the main menu works fine up until i access a option wioth another INPUT command
Code:
IF choice$ = "a" THEN GOSUB Stats
IF choice$ = "a" THEN GOSUB Inventory
IF choice$ = "a" THEN GOSUB Town
IF choice$ = "a" THEN GOSUB Battle
IF choice$ = "a" THEN GOSUB Quit
Dunno if you meant to put "a" for each of them..

but.. try this.
Code:
SELECT CASE choice$
CASE "a": GOSUB Stats
CASE "b": GOSUB Inventory
CASE "c": GOSUB Town
CASE "d": GOSUB Battle
CASE "e"; GOSUB Quit
END SELECT
i meant a,b,c,d,e
but anyway it does the same thing, doesnt it, anyway ill try it for fun l8er
At the top of the menu, you should not test the variable choice$ before you ever read it.

Using WhiteTiger's suggestions, I'm changing your code as follows. Notice the changes. I hope you agree.
Code:
DO
  CLS
  PRINT "What would you like do do next?"
  PRINT
  PRINT "a. Stats"
  PRINT "b. Inventory"
  PRINT "c. Town"
  PRINT "d. Battle"
  PRINT "e. Quit"
  INPUT choice$
  choice$ = lcase$(left$(choice$,1))
  SELECT CASE choice$
         CASE "a": GOSUB Stats
         CASE "b": GOSUB Inventory
         CASE "c": GOSUB Town
         CASE "d": GOSUB Battle
         CASE "e": GOSUB Quit : SYSTEM
  END SELECT
LOOP
Quote:but anyway it does the same thing, doesnt it
No I don't believe so. With yours it checks choice$ over and over again for each one. with SELECT CASE it's only once.

Plus it's easier to look at =P
your problem may be in the actual sub label

Code:
GOSUB test

print "completed sub `test`"

end

test:
print "moooooooooo"

return   ' Magic of basic syntax - return to where GOSUB was called.

that may just be the problem

oz~
Pages: 1 2