Hi All
In a program I call the RequestStr command to get user input.
Say the user just presses the OK button without inputting anything, and I then check
for an empty string, "", as the input.
This causes an "Argument error" response.
Can I check if the user pressed the "OK" button or "cancel" button so that I can write code according to the button pressed.
Any help would be greatly appreciated.
tgahan
You are encountering 2 different problems here. The first one is the case where the user presses Cancel instead of Ok. Here's how you can check for that:
You can provide 4 arguments to the RequestStr command, only 2 of which are mandatory.
You can do something like this:

Code:
RequestStr "Enter Something",result,0,ab

The parameters are as follows:
"Enter Something" is the string displayed in the input box
result is the variable that contains the user's input
0 tells the program not to display the line containing the user's input
ab is a variable that will contain a 0 if the user pressed "Cancel" and a 1 if the user pressed "Ok".
A common thing to do in nspire basic is to follow up an input with a quick check to make sure the user didn't press Cancel like this:

Code:
If ab=0 Then
   //End the program
   Goto End
ElseIf ab=1 Then
   //Continue
Endif
Lbl End

Note that you generally don't want to put a Goto in an if block because this can cause a memory leak, but this doesn't really matter in this case since you are ending the program.

Your second problem is the case where the user presses Ok, but has left the input box empty. You can mitigate this by wrapping your RequestStr command in a Try:Else:EndTry block like this:

Code:
Try
RequestStr "Enter Something",result,0,ab
Else
   //User left the input box empty
   Goto End
EndTry
Lbl End


Combining these two things gives you the following:

Code:
Try
RequestStr "Enter Something",result,0,ab
Else
   //User left the input box empty
   Goto End
EndTry
If ab=0 Then
   //User pressed Cancel
   Goto End
ElseIf ab=1 Then
   //User entered something and pressed Ok
Endif
Lbl End
Hi mr womp womp
Thank you so much for your detailed explanation.
This is exactly what I wanted.
I saw this reference to the fourth argument [status var] of the Request command in the TI Nspire reference guide (4.5) but [status var] is not listed as an optional argument to the RequestStr command in the guide and since RequestStr was the command I wished to use, I assumed argument [status var] did not apply to this command. Should have just tried it anyway. The guide presumably is only detailing the difference in return types of both commands.
Once again thank you so much.
regards
tgahan
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement