Write a program that it only admits people between the ages of 18 and 25

Started by kevin, August 24, 2022, 09:24:45 PM

Previous topic - Next topic

kevin

QUESTION:

  Write a PlayBASIC program that it only admits people between the ages of 18 and 25





ANSWER:


   Bellow is solution that uses the STATICINPUT function for text input.   This simulates the classic INPUT statement in traditional BASIC, but using a modern graphics screen.  As such we need to include the library and clear the screen and refresh the screen.  The actual logic of the program boils down to is a few IF / THEN comparisons.   So once we have had input from the user (while will be in text for) we need to remove any unwanted characters, then convert it to an Integer using VAL command.  Once converted we can perform numeric comparisons on the newly enter age.   


   The program will keep running until you enter nothing or type the words END or QUIT

PlayBASIC Code: [Select]
   ; Include the Input Library.  This gives us the StaticInput function
#Include "input"


People_Inside = 0


; ------------------------------------------------------------
; Start of MAIN DO/LOOP - This will keep the program running
; until you enter quit to end
; ------------------------------------------------------------
do


; clear the graphics screen to black
Cls rgb(0,0,0)

; display instructions
print "Type END or QUIT to end program"
print "People Between (18 to 25) that are inside:"+str$(People_Inside)
print ""
print ""


; Ask the user to input some text. In this case we're asking their age
; but remember since this is TEXT they could return anything !
Answer$=StaticInput("Enter your age ?")


; Trim any spaces or tabs from the what they entered
Answer$=trim$(Answer$)


; Get the Value of the Age$ as an Integer for comparisons
Age = val(Answer$)


; Check if this AGE is within our required age group
; we check if age is both equal to or greater than 18
; and age is lower than or equal to 25
if (Age >=18) and (Age<=25)

print "Thanks.. you're allowed in!"
else

; Show incvalue AGE reanges..
if Age<18
print "Sorry you're too young"
endif

if Age>25
print "Sorry you're too old"
endif



endif

; Refresh the graphics screen so the user can see it
Sync

wait 500


// convert the answer string to all lower case letters
Answer$=lower$(Answer$)


; Loop back to DO, but only do it if Answer$ isn't "END", "QUIT" or
; an empty string
; ------------------------------------------------------------
loop Answer$="" or Answer$="end" or Answer$="quit"
; ------------------------------------------------------------

end









    LINKS:

          Read HELP FILES about IF / THEN Comparisons
         Read HELP FILES about Operators


   #LearnToCode  #LearnBASIC LearnProgramming #LearnCoding