The QBasic Forum      Other Subforums, Links and Downloads
 
  << Previous Topic | Next Topic >>Return to Index  

A requested auto month / date input routine

March 31 2009 at 11:39 AM
Pete  (Login The-Universe)
Admin

This program automatically inputs the month and year (no Enter key required) and checks them. Use the arrow up key if you goof up the month to redo it. Also, if you need months 10, 11, or 12, simply backspace over the leading zero before you type them.

Pete

----------------------------------------------

REM Example input routine. Automatically inputs after characters are typed.
CLS
PRINT "Month: "
PRINT "Year:" + SPACE$(2);
LOCATE 1, 8, 1
DO
entries% = entries% + 1
IF entries% = 1 THEN numofspaces% = 2 ELSE numofspaces% = 4
startpos% = POS(1)
endpos% = startpos% + numofspaces%
DO
DO
IF b$ = CHR$(13) THEN EXIT DO
b$ = INKEY$
IF entries% = 1 AND word$ = "" AND auto% = 0 THEN auto% = 1: b$ = "0"
IF b$ <> "" THEN EXIT DO
LOOP
xx% = CSRLIN: yy% = POS(1)

SELECT CASE b$
CASE CHR$(0) + "K"
mov% = -1: GOSUB action
CASE CHR$(0) + "M"
mov% = 1: GOSUB action
CASE CHR$(0) + "H"
IF entries% = 2 THEN word$ = "": entries% = entries% - 2: LOCATE entries% + 1, 8: PRINT SPACE$(2); : LOCATE entries% + 1, 8: auto% = 0: EXIT DO
CASE CHR$(0) + "S"
GOSUB wash: GOSUB delete
CASE CHR$(0) + "R"
IF ins% = 0 THEN ins% = -1 ELSE ins% = 0
IF ins% = 0 THEN LOCATE , , 1, 7, 7 ELSE LOCATE , , 1, 7, 30
CASE CHR$(0) + "O"
IF word$ <> "" THEN LOCATE xx%, startpos% + LEN(word$)
CASE CHR$(0) + "G"
LOCATE xx%, startpos%
CASE CHR$(8)
IF yy% > startpos% THEN
LOCATE , POS(1) - 1
yy% = POS(1)
GOSUB wash: GOSUB delete
END IF
CASE CHR$(13)
REM Enter your routine here.
SELECT CASE entries%
CASE 1
IF VAL(word$) < 1 OR VAL(word$) > 12 THEN SOUND 100, 1: entries% = 0: word$ = "": LOCATE entries% + 1, 8: PRINT SPACE$(2); : LOCATE entries% + 1, 8: auto% = 0: b$ = "": EXIT DO
month$ = word$
CASE 2
IF VAL(word$) < 1000 THEN SOUND 100, 1: entries% = 1: word$ = "": LOCATE entries% + 1, 8: PRINT SPACE$(4); : LOCATE entries% + 1, 8: auto% = 0: b$ = "": EXIT DO
year$ = word$
END SELECT
IF entries% = 1 THEN LOCATE entries% + 1, 8
word$ = "": key$ = "": b$ = "": auto% = 0
EXIT DO
CASE CHR$(27)
COLOR 7, 0: SYSTEM: REM ends program when Esc key is pressed.
CASE CHR$(32) TO CHR$(126)
key$ = b$: GOSUB action
END SELECT
mov% = 0: key$ = ""
LOOP
LOOP UNTIL entries% = 2
PRINT : PRINT
PRINT "Month Typed = "; month$
PRINT "Year Typed = "; year$
SYSTEM

action:
IF POS(1) + mov% >= startpos% AND POS(1) + mov% < endpos% THEN
DO
IF key$ <> "" THEN
SELECT CASE ins%
CASE 0
IF LEN(word$) + LEN(key$) > endpos% - startpos% THEN EXIT DO
word$ = MID$(word$, 1, POS(1) - startpos%) + key$ + MID$(word$, POS(1) - startpos% + 1)
CASE -1
word$ = MID$(word$, 1, POS(1) - startpos%) + key$ + MID$(word$, POS(1) - startpos% + 2)
END SELECT
END IF
IF POS(1) - startpos% >= LEN(word$) - LEN(key$) AND key$ <> "" OR key$ = "" OR ins% = -1 THEN
IF key$ = "" AND mov% = 1 THEN IF POS(1) - startpos% = LEN(word$) THEN EXIT DO
LOCATE , POS(1) + mov%: PRINT key$;
ELSE
LOCATE xx%, startpos%: PRINT MID$(word$, 1, yy% - startpos%); key$; : yy2% = POS(1): PRINT MID$(word$, yy% - startpos% + 2); : LOCATE xx%, yy2%
END IF
EXIT DO
LOOP
END IF
IF LEN(word$) = numofspaces% THEN b$ = CHR$(13): REM triggers automatic 4-digit input.
RETURN

wash:
IF POS(1) >= startpos% AND word$ <> "" AND POS(1) - startpos% < LEN(word$) THEN
LOCATE xx%, startpos% + LEN(word$) - 1
PRINT " ";
LOCATE xx%, yy%
END IF
RETURN

delete:
IF POS(1) - startpos% = 0 THEN
word$ = MID$(word$, 2)
ELSE
word$ = MID$(word$, 1, POS(1) - startpos%) + MID$(word$, POS(1) - startpos% + 2)
END IF
PRINT MID$(word$, yy% - startpos% + 1); : LOCATE xx%, yy%
RETURN


 
 Respond to this message   
AuthorReply

(Login burger2227)
R

A timed entry loop

March 31 2009, 1:43 PM 

I use a SUB program similar to this to change chapters in a demo. It will wait up to 2 seconds for another entry when a 1 is pressed.

SUB Entry (chp%) 'chp% returned to program

DO: A$ = INKEY$: LOOP UNTIL A$ = "" 'clear key buffer
DO: CH$ = INKEY$: LOOP UNTIL CH$ > CHR$(48) AND CH$ < CHR$(58) 'wait for first non-zero entry
start! = TIMER: dlay! = start! + 2 '2 second delay for second entry
DO:
IF CH$ = CHR$(49) OR done = 1 THEN 'if 1 is entered or second digit
IF CH$ > CHR$(47) AND CH$ < CHR$(51) THEN 'limit 0 to 2
one$ = one$ + CH$
done = done + 1 '2 if sec digit entered
END IF
ELSEIF CH$ > CHR$(49) AND CH$ < CHR$(58) THEN 'limit 2 to 9
LOCATE 20, 68: PRINT CH$ 'exit if entries above 1
chp% = VAL(CH$): EXIT DO 'convert to number? and quit
END IF
CH$ = INKEY$ 'get second digit?
LOCATE 20, 68: PRINT one$
chp% = VAL(one$) 'convert string to number value?
IF start! > TIMER THEN dlay! = dlay! - 86400 'midnite adjustment
LOOP UNTIL TIMER > dlay! OR done = 2
SLEEP 1 'allow user to view entry
A$ = INKEY$ 'clear SLEEP key buffer if used

END SUB

I set this up for numbers from 1 to 12 only. I convert it to a number value for a chapter in SELECT CASE. You could use the string value also.
OOPS! Edited LOCATE's for screen 0. I had screen 12.

Ted


    
This message has been edited by burger2227 on Mar 31, 2009 4:04 PM
This message has been edited by burger2227 on Mar 31, 2009 3:19 PM


 
 Respond to this message   

(Login The-Universe)
Admin

For fun, a very cut down version using INPUT$(1) with no checking or going back.

March 31 2009, 6:21 PM 

CLS : LOCATE , , 1
PRINT "Month: ": PRINT "Year: "; : LOCATE 1, 8
DO
i = 0: num% = num% + 2
DO
a$ = INPUT$(1)
IF a$ = CHR$(27) THEN SYSTEM
IF VAL(a$) > 0 AND VAL(a$) <= 9 OR a$ = "0" THEN
i = i + 1: PRINT a$;
IF num% = 2 THEN Month$ = Month$ + a$ ELSE Year$ = Year$ + a$
END IF
LOOP UNTIL i = num%
PRINT : LOCATE , 8
LOOP UNTIL num% = 4
PRINT : PRINT "Month = "; Month$: PRINT "Year = "; Year$
SYSTEM

 
 Respond to this message   
Current Topic - A requested auto month / date input routine
  << Previous Topic | Next Topic >>Return to Index  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums