@Super Guy:
Quote: "nonZero, can you please explain your code to me... I am a real beginner."
Sure. Here's the code, this time with comments. I'm not always brilliant at explaining things, I hope I've clarified this.
`Modified example. The original example jumps around a lot and I THINK is actually
`to showcase the GOTO command. Anyway, here's the dissection of my modifications
`(You'll notice my code's keywords are in UPPERCASE. It's not just for the example,
`it's genuinly a regression I made back to my old-school days with QBasic, lol):
print "" `Initially printing text to screen.
print " Do you..." `Printing nothing is a quick way to skip a line.
print " 1.) Hide under your bed until the noise go away...?"
print " 2.) Calmly answer the door...?"
print " 3.) Threaten to attack whoever it is...?"
PRINT " "
INK RGB(250,250,0), RGB(0,0,0) `Here's where I make the choice yellow, just for fun.
PRINT " Make your choice (1-3)..." `For more info, see the RGB() and INK commands.
chosen = 0 `Now we have a flag. This is a variable used as a codition to determine an event
WHILE chosen = 0 `WHILE..ENDWHILE is similar to DO..LOOP however it is more efficient because it only enters the loop if the condition is true to begin with.
chosen = VAL(INKEY$()) `Okay, here's me using shortcuts again. Now the INKEY$() function returns the key pressed as a string.
`VAL() function returns the numeric value of a string so if a$ = "123" then VAL(a$) = 123. If the string's invalid, 0 is returned.
`So I'm assigning the numeric value of the string returned by INKEY$() to my flag (chosen).
`Therefore a non-numeric input will give you a value of 0 and the loop will continue.
`However ANY value other than 0 will exit the loop, ie 4,5,6,7,8,9
ENDWHILE
SELECT chosen `A CASE SELECT is essentially a slightly more effecient set of IF statements.
CASE 1 `IF chosen = 1
retstring$ = " You hid under your bed but the boogeyman ate you." `set the return string variable's value to " You hid....etc"
ENDCASE `ENDIF
CASE 2 `Same with the next step, and so on.
retstring$ = " You answered the door to find a pony in front of you."
ENDCASE
CASE 3
retstring$ = " A dark voice from beyond the door laughs away your threat."
ENDCASE
ENDSELECT
WHILE SCANCODE() > 0: ENDWHILE `A force-of-habit after checking for inputs is to loop until there are none
`In this cas, I used SCANCODE() which returns the numeric value of the key being pressed.
`This is different from INKEY$() as INKEY$() returns the character. A SCANCODE() is a number that is
`used to identify each key. If the SCANCODE() reads 0, then no key is being pushed.
CLS `Clear the screen.
INK RGB(200,50,50), RGB(0,0,0) `Set colour to a darkish red.
PRINT " " `Skip a space.
PRINT retstring$ `Now print the return string variable. Remember that this is affected by the CASE SELECT above
`so we only need this one statement instead of 3 functions/subs/labels, each containing a PRINT statement.
PRINT " "
INK RGB(250,250,250), RGB(0,0,0) `Set colours back to white
PRINT " Press any key to proceed to chapter 2..." `Print standard end-of-example text.
WAIT KEY `WAIT KEY is a DBPro function which loops until a key if pressed.
END `END terminates the program. Not necessary in this example, but it's a good habbit to make.
Quote: "if I still used the exact same method (the one in the OP) how would I make it only work with the 3 numbers?"
The easiest way is to trap the INPUT statement in a loop and only exit if the answer given is within a determined range, 1-3 in this case, so:
DO
PRINT " "
INPUT " Enter choice (1-3) ", InputString$
IF VAL(InputString$) > 0 AND VAL(InputString$) < 4
EXIT
ELSE
PRINT " "
PRINT "Invalid! You messin' with me boy?"
ENDIF
LOOP
Note the use of flow. Manipulating program flow can save you lots of time and that's why ELSE is an indispensable part of BASIC.
ON A SIDE-NOTE:
If you enter "2" then it goes to "AnswerDoor:" and "ThreatenAttack:". It's because the example posted is missing an "END" at the
end of the label "AnswerDoor:" (And "ThreatenAttack:" but this won't affect it as the program reaches its end here). This is why GOTO is not good for beginners and I really wish these folk who write tutorials would just leave it out. GOTO is a WMD for performance but it is a dangerous command for many reasons. GOSUB is a better alternative (though personally, I prefer Functions because it makes linear, unified code. But that's just my preference, you must find your own - so long as it doesn't involve using GOTO, lol).
Here's the original code with numbers in the print statement:
print ""
print " Do you..."
print " 1.) Hide under your bed until the noise go away...?"
print " 2.) Calmly answer the door...?"
print " 3.) Threaten to attack whoever it is...?"
INPUT "Enter choice (1-3) ", InputString$
if InputString$= "1" then goto HideBed
if InputString$= "2" then goto AnswerDoor
if InputString$= "3" then goto ThreatenAttack
HideBed:
print ""
print "1..."
wait key
end
AnswerDoor:
print ""
print "2..."
wait key
ThreatenAttack:
print ""
print "3..."
wait key