Author Topic: Multiple ANDs have tangled my brain...  (Read 4097 times)

0 Members and 1 Guest are viewing this topic.

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Multiple ANDs have tangled my brain...
« 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

 

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Multiple ANDs have tangled my brain...
« Reply #1 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.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #2 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.  
« Last Edit: September 28, 2020, 11:41:34 am by bplus »

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #3 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

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #4 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%%
Granted after becoming radioactive I only have a half-life!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #5 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

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Multiple ANDs have tangled my brain...
« Reply #6 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!
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #7 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #8 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.
« Last Edit: September 28, 2020, 02:40:15 pm by Richard Frost »
It works better if you plug it in.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Multiple ANDs have tangled my brain...
« Reply #9 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$
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #10 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.
« Last Edit: September 28, 2020, 03:55:04 pm by bplus »

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #11 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.

It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #12 on: September 28, 2020, 07:46:10 pm »
Dang I thought cavemen were the first users of Basic.

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: Multiple ANDs have tangled my brain...
« Reply #13 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