QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: MWheatley on September 28, 2020, 10:27:09 am

Title: Multiple ANDs have tangled my brain...
Post by: MWheatley on September 28, 2020, 10:27:09 am
I'm attempting to straighten out some 1980s code so as to eliminate a few GOTOs and make the whole thing a bit more linear.

The line below is supposed to check that an INPUTted string (text$) doesn't contain either an uppercase or lowercase 'Y' or 'N'.

Quote
IF (text$ <> "y" AND text$ <> "Y") AND (text$ <> "n" AND text$ <> "N") THEN linenumber = 20

Except that it doesn't: it fails heroically.

What have I done wrong?!  TIA.

Malcolm

 
Title: Re: Multiple ANDs have tangled my brain...
Post by: SpriggsySpriggs on September 28, 2020, 10:45:07 am
You could do this instead to get rid of some of that:

Code: QB64: [Select]
  1. IF UCASE$(text$) <> "Y" AND UCASE$(text$) <> "N" THEN linenumber = 20

I tested it just now and it works.
Title: Re: Multiple ANDs have tangled my brain...
Post by: bplus on September 28, 2020, 11:32:12 am
Code: QB64: [Select]
  1. IF (text$ <> "y" AND text$ <> "Y") AND (text$ <> "n" AND text$ <> "N") THEN linenumber = 20

This is the kind of line that if you don't enter a Y, y, N, n for a yes or no question then the code is supposed to loop around and ask the dang question again. So the meaning of that line is: did you enter with a y or n keypress and if not I will ask again.

So if it fails,it probably fails to loop back to the question. Have to see more code to see how linenumber is used? but I expect you want a GOTO to a line, maybe line 20?, just above the INPUT question to ask again the yes or no question.

DEMO, line does work when connected to proper GOTO
Code: QB64: [Select]
  1.  
  2. 20 INPUT " Press Y or y for Yes and N or n for no "; text$
  3. IF (text$ <> "y" AND text$ <> "Y") AND (text$ <> "n" AND text$ <> "N") THEN GOTO 20
  4. PRINT "Congratulations you pressed Y or y or N or n,  goodbye"
  5.  
  6.  
Title: Re: Multiple ANDs have tangled my brain...
Post by: MWheatley on September 28, 2020, 11:47:05 am
Thanks, both.  Bplus, I now see the problem -- cheers.  And SS, that's a neat idea.

Malcolm
Title: Re: Multiple ANDs have tangled my brain...
Post by: Cobalt on September 28, 2020, 12:19:12 pm
and just another way to do it..

Code: QB64: [Select]
  1.  INPUT text$
  2.  SELECT CASE test$
  3.   CASE "Y","y","n","N"
  4.    EXITFLAG%%=-1
  5. LOOP UNTIL EXTIFLAG%%
Title: Re: Multiple ANDs have tangled my brain...
Post by: Pete on September 28, 2020, 12:52:48 pm
Code: QB64: [Select]
  1. INSTR(UCASE$(text$), "Y") = 0 AND INSTR(UCASE$(text$), "N") = 0 THEN whatever = whatever

Pete

Title: Re: Multiple ANDs have tangled my brain...
Post by: SpriggsySpriggs on September 28, 2020, 01:07:01 pm
I realize now I didn't read the part about GOTO. I would have added GOTO 20 instead of linenumber = 20. Apologies! I was just converting the first portion of the statement haha!
Title: Re: Multiple ANDs have tangled my brain...
Post by: Pete on September 28, 2020, 01:19:03 pm
I realize now I didn't read the part about GOTO. I would have added GOTO 20 instead of linenumber = 20. Apologies! I was just converting the first portion of the statement haha!

:D :D :D That's why I used the variable: whatever. Hey, would it have been funnier if I made it LONG explicit? whatever!

Pete
Title: Re: Multiple ANDs have tangled my brain...
Post by: Richard Frost on September 28, 2020, 02:38:49 pm
Here's my twisted method:

Code: QB64: [Select]
  1.     i$ = INKEY$
  2.     IF i$ = "" THEN i$ = " "
  3.     selected = INSTR("NY", UCASE$(i$)) ' Iiiiii like New York in June, how about youuuuu?
  4. LOOP UNTIL selected
  5. PRINT MID$("NY", selected, 1)
  6.  

Nulls (nothing pressed) have to be taken care of, as shown, because INSTR always returns 0 for a null.
Title: Re: Multiple ANDs have tangled my brain...
Post by: SMcNeill on September 28, 2020, 02:51:54 pm
I tend to just make these things into a quick little function...

Code: QB64: [Select]
  1. Result$ = WaitFor("YyNn", "Press <Y> or <N> to continue.")
  2. PRINT "You chose: "; Result$
  3.  
  4.  
  5. FUNCTION WaitFor$ (Acceptable$, Prompt$)
  6.     PRINT Prompt$;
  7.     DO
  8.         WaitFor$ = INPUT$(1)
  9.     LOOP UNTIL INSTR(Acceptable$, WaitFor$)
  10.     PRINT WaitFor$
Title: Re: Multiple ANDs have tangled my brain...
Post by: bplus on September 28, 2020, 03:36:02 pm
If you are going to do this sort of thing allot in a program I do this:

Code: QB64: [Select]
  1. PRINT "Here is my getChar$() Function that waits until you hit certain keys to exit."
  2. PRINT "The advantage, you don't have to Enter the key, the disadvantage it responds"
  3. PRINT "immediately, no 2nd thoughts allowed like with INPUT that requires Enter."
  4. PRINT "Would you like to try the demo?"
  5. PRINT " Y or y for Yes, N or n for No, escape for get me outta here!"
  6. choice$ = getChar$("YyNn" + CHR$(27))
  7. IF ASC(choice$) = 27 THEN
  8.     PRINT "Goodbye!": END
  9. ELSEIF choice$ = "Y" OR choice$ = "y" THEN
  10.     PRINT "You would like to try the demo."
  11.     PRINT "So do you like it so far?"
  12.     PRINT "Press l for Like and y for yuck! or escape to cancel."
  13.     choice2$ = getChar$("lLyY" + CHR$(27))
  14.     IF ASC(choice2$) = 27 THEN
  15.         PRINT "Goodbye!": END
  16.     ELSE
  17.         IF choice2$ = "l" OR "L" = choice2$ THEN
  18.             PRINT "Great! we will quit now before  you change your mind ;-))"
  19.         ELSE
  20.             PRINT "Sorry, have a nice day."
  21.         END IF
  22.     END IF
  23.     PRINT "Dang! you don't want to try the demo."
  24.  
  25. FUNCTION getChar$ (fromStr$)
  26.     DIM OK AS INTEGER, k$
  27.     WHILE OK = 0
  28.         k$ = INKEY$
  29.         IF LEN(k$) THEN
  30.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  31.         END IF
  32.         _LIMIT 200
  33.     WEND
  34.     _KEYCLEAR
  35.     getChar$ = k$
  36.  
  37.  

Oh dang this is redundant, I saw INPUT in Steve's and assumed the other one.
Title: Re: Multiple ANDs have tangled my brain...
Post by: Richard Frost on September 28, 2020, 04:52:41 pm
A truly advanced input function would detect a caveman's answer -
one grunt for no, two grunts for yes.  But I don't know how to access
the microphone.

A huge benefit of INKEY$ over INPUT$ is that you can be processing
things, like animating a rubber chicken walking across the screen,
while waiting for the user to do something. 

http://www.qb64.org/wiki/Cavemen

And no, *I* didn't do that Wiki entry.  I don't even have access.

Title: Re: Multiple ANDs have tangled my brain...
Post by: bplus on September 28, 2020, 07:46:10 pm
Dang I thought cavemen were the first users of Basic.
Title: Re: Multiple ANDs have tangled my brain...
Post by: MWheatley on September 29, 2020, 05:00:54 pm
Thanks again, folks.  Food for thought.

The vast majority of my numeric inputs get passed to a long-established subroutine that works like a charm, although I've recently had to tweak it to accept zero values as genuine.

Pure alphameric input in my programs is unusual, and in the original 1980s code, things worked.  (For reasons made clear here.)

There have been some useful suggestion in this thread, and with a couple of further programs on the stocks, I will be trying them out.  Call it the Covid-19 dividend: after many years of almost zero programming, there's something of a personal coding flurry underway at present!

Malcolm