Getting a user response

by Celestianpower

In my staple tutorial, we discussed the input statement but do you really know how to get a user response and use it? No? I thought not. Well, I'm gong to divulge some tips about how to get your user back in control.

Input

First off, I think we need to do a bit of revision. You should already know (and use incessantly) this line of code:

 	input "question"; answer_variable

Yes? Right, so you understand inherently what this bit of code does:

 	input "What's your name? "; name$
	print "Your name is "; name$; "."

This will ask for you're name and display it in the next sentence.

This is all well and good but it's pretty silly if we don't get the program to check what it is. Try this:

 	input "What's your name? "; name$
	print "Your name is "; name$; "."
	if name$ = "Celestianpower" then print "That's my name too!!!!"

Now we're going places. Not only have we grabbed the response of the user,we have evaluated it too. However, we can go further. JustBASIC is case sensitive: Celestianpower is different to celestianpower. to eliminate these sorts of discrepancies, we can use the upper$() and lower$() commands. Try this:

 	input "What's your name? "; name$
	print "Your name is "; name$; "."
	if lower$(name$) = "celestianpower" then print "That's my name too!!!!"

Notice that "celestianpower" is now in lower case. This is because we are converting the answer to lower case (lower$(name$)) so we need to compare it with a lower case version of the name.

Okay, with me so far? Good. I think we've had enough of such wide questions and we should do some multiple choice. Have a look at this:

 	input "Are you 13 years old? "; ans$
	if ans$ = "yes" then 
	print "You are 13 years old."
	else
	print "You aren't 13 years old."
	end if

Firstly, don't get put off by the long version of if ... then. Just look back at my staple tutorial to get you're head round it if you don't understand. Have you noticed the problem with this yet? It the person puts "Yes", "yeah" or just "Y" then it won't work. There are a few things we could do to combat this (replace the if ans$ = "yes" then line for any of these and see what happens):

 	if instr("YESyesYyYEAHyeah", ans$) then

	if upper$(ans$) = "YES" then
	
	if left$(upper$(ans$), 1) = "Y" then

At this point, I should probably explain what these things do. The first one searches for the answer (ans$) in the string of various answers ("YESyesYyYEAHyeah"). The problem with this method is that if the person (silly as it might sound) types, "Yes, but I'm nearly 14" then it recognizes it as not 13 because "Yes, but I'm nearly 14" doesn't appear in the list you gave it. You could be there for hours putting in different possibilities.

The second one we've met before in if lower$(name$) = "celestianpower". if the user puts in "Yeah" or "yep" then it'll just think it's been told "no" and will tell the user that they're not 13.

The third one is my favourite and I think the most foolproof. It compares the upper case version of the first letter to "Y". This means that all possibilities are covered, "Yes", "Yeah" and "yep".

Now, without getting too bogged down in how to do functions, we can do this:

     input "Are you 13 years old? "; ans$
    if yn$(ans$) = "Y" then
        print "You are 13 years old."
    else
        print "You aren't 13 years old."
    end if
end

function yn$(answer$)
    if left$(upper$(answer$), 1) = "Y" then
        yn$ = "Y"
    else
        yn$ = "anything other than Y"
    end if
end function

We have just set up a function to do the left$(upper$(ans$), 1) for us. The advantage of doing this is that we only have to type the left$(upper$(ans$), 1) out once in the function and just type yn$(ans$) = "Y" in our if statement. It declutters our code and saves us hassle in debugging.

Now, this is about all we can say about inputs but as we will see next, it's made a lot easier if we can physically restrict what the user can say using confirm.

Confirm

Confirm is a dialog box that looks like this:

And it's syntax is:

 
	 confirm "Question"; answer_variable$

As you can see, this is remarkably similar to the input command and works the same too. The MAJOR advantage is that if the user clicks the yes button, the answer_variable$ is set as "yes" and if the user clicks "no" then the answer_variable$ is set to "no". this means that we can just use ifs with none of the solutions given above:

 
	 confirm "Are you 13 years of age?"; ans$
	 if ans$ = "yes" then
	 	print "You are 13 years old."
	else
		print "You aren't 13 years old."
	end if

See, easy isn't it?

Prompt

And finally, we come to prompt. Here's the syntax:

 
	 prompt "Titlebar" + chr$(13) + "Question"; answer_variable$

As you can see, this is slightly different. You can add some text into the title bar (antything in the first set of question marks goes there). It looks like this:

You can use this for things like names and ages but it's better to use confirm for yes/no questions. It's all about choosing the best function for the job.