Qbasicnews.com
July 20, 2019, 11:25:30 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to put the time and date into your program so that it runs regardless.  (Read 5047 times)
Smurfy
New Member

Posts: 11


« on: October 17, 2007, 01:48:20 AM »

I know the general

DO
PRINT TIME$
SLEEP 1
CLS
LOOP

thing but I can't get it to work for some reason.

I'm sure theres a better way of having it do this for me, I just need it to update every second and have the date change over at midnight.
 Can anyone give me a solution?
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #1 on: October 17, 2007, 05:15:07 PM »

Try:
Code:
DO
  CLS
  PRINT DATE$; "  "; TIME$
LOOP
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Smurfy
New Member

Posts: 11


« Reply #2 on: October 17, 2007, 09:37:50 PM »

Well that didn't work but I wasn't surprised, I should have specified.
  I also want to add the current up to the second date and time in my program.
  It starts out with a menu that then takes you to two choices whether you want to compute a single year or a range of years, thats off subject though i just cant figure out how to put it in so thats it runs throughout my program updating every second without me having to hit any keys. Can anyone give me a solution?
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #3 on: October 18, 2007, 12:29:06 AM »

Perhaps you can make a Sub to just do the updating, without a DO...LOOP, then, throughout your program, call that sub, from time to time?  Instead of using the CLS command, you can use a two LOCATEs, the first one to erase the previous date and time, the second one to write the new date and time.  Give it a try, then, if you still have a problem, post back, with the code you came up with, and explain your problem. 
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Mac
Senior Member
**
Posts: 243


WWW
« Reply #4 on: October 18, 2007, 05:11:42 PM »

if you still have a problem, post back, with the code you came up with, and explain your problem. 

Yes, and take a LOT of time to explain the problem adequately. Just to blurt out a question at the speed of chatroom is worthless and will never get you any help. Spend at least 15 minutes to compose your next question.

From your original question, I would have responded
CLS
LastTime$ = TIME$
DO
LOCATE 10, 30: PRINT DATE$, LastTime$, ""
  WHILE TIME$ = LastTime$: WEND
  LastTime$ = TIME$
LOOP WHILE INKEY$ = ""


Mac

Logged
Smurfy
New Member

Posts: 11


« Reply #5 on: October 18, 2007, 05:20:51 PM »

In my menu I put in
        GOSUB TimeandDate
after the input statement in my menu that asks "Would you like to calculate a range of years(1) or a single year(2)?";Q$ but before the part where it says
        IF VAL(Q$) = 1 THEN  GOTO Range
        IF VAL(Q$) = 2 THEN GOTO SingleYear
        IF VAL(Q$) < 1 OR VAL(Q$) > 2 THEN GOTO Menu

TimeandDate:
        LOCATE 1, 1
        PRINT DATE$
        LOCATE 1, 1
        PRINT DATE$
        LOCATE 1, 72
        PRINT TIME$
        LOCATE 1, 72
        PRINT TIME$
        RETURN        
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #6 on: October 18, 2007, 05:29:35 PM »

Well, you still don't explain what your problem is. Did you spend 15 minutes on that question? If so, try 30 minutes.

Making a wild guess WTF you are talking about, I jotted out the program below. Try running it.

Mac

ON TIMER(1) GOSUB UpdateTime

CLS
PRINT "Welcome to fancy program"
TIMER ON
WHILE row = 0: WEND
PRINT : PRINT "Press 'A': ";
WHILE UCASE$(INKEY$) <> "A": WEND
PRINT "You did good!"
PRINT : PRINT "Now press 'B': ";
WHILE UCASE$(INKEY$) <> "B": WEND
CLS
SYSTEM

UpdateTime:
row = CSRLIN
col = POS(1)
LOCATE 1, 60: PRINT DATE$; " "; TIME$
LOCATE row, col
RETURN

Logged
Smurfy
New Member

Posts: 11


« Reply #7 on: October 18, 2007, 07:08:42 PM »

That works but I don't know how to integrate that into my program.
Code:
'Leapyear Calculator
'Written by: Alex Willett
'October 2007




CLS
Menu:
  CLS
  LOCATE 2, 1
  INPUT "Would you like to compute a range of years(1) or a single year(2)"; Q$
  GOSUB TimeandDate
  IF VAL(Q$) = 1 THEN GOTO Range
  IF VAL(Q$) = 2 THEN GOTO SingleYear
  IF VAL(Q$) < 1 OR VAL(Q$) > 2 THEN GOTO Menu
  ON TIMER(1) GOSUB TimeandDate

Range:
   INPUT "What is the first year"; F$
   INPUT "What is the last year"; L$
   F = VAL(F$)
   L = VAL(L$)
   FOR X = F TO L
   IF X MOD 400 = 0 OR (X MOD 100 = 0 OR X MOD 4 = 0) THEN
   COLOR 7, 0
   PRINT X; "is a leapyear!"
   ELSE
   COLOR 4, 0
   PRINT X; "is not a leapyear."
   END IF
   R = TIMER
   DO
   LOOP WHILE TIMER < R + .1
   NEXT X
   GOSUB PressAny
   GOTO Response


SingleYear:
   CLS
   y = 0
   INPUT "Enter a year"; y
   IF y < 1582 THEN
   M = 1
   END IF
   IF y = 1582 THEN
   M = 1
   END IF
   IF y MOD 4 = 0 THEN
   M = 1
   END IF
   IF y MOD 100 = 0 THEN
   M = 2
   END IF
   IF y MOD 400 = 0 THEN
   M = 1
   END IF
   IF M = 1 THEN
   PRINT y; "is a leap year."
   END IF
   IF M > 1 OR M < 1 THEN
   PRINT y; "is not a leap year."
   END IF
   GOSUB PressAny
   GOTO Response

Response:
   CLS
   INPUT "Would you like to compute another year"; R$
   IF R$ = "Y" OR R$ = "y" THEN GOTO Menu
   IF R$ = "N" OR R$ = "n" THEN GOTO ShutDown

TimeandDate:
   row = CSRLIN
   col = POS(1)
   LOCATE 1, 60: PRINT DATE$; " "; TIME$
   LOCATE row, col
   RETURN


PressAny:
   DO
   I$ = INKEY$
   LOOP WHILE I$ = ""

Shutdown:
  CLS
  END
Logged
Smurfy
New Member

Posts: 11


« Reply #8 on: October 18, 2007, 08:35:33 PM »

I can put a clock in a standalone program, and I can put a clock in my menu but that causes my menu to stop working and this program above did exactly the same thing.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #9 on: October 18, 2007, 10:09:34 PM »

This is what you need for your "date change over at midnight".

BEWARE OF MIDNIGHT:
It is possible that your program is operating at midnight.

If you are about 1 second before midnight when obtaining the date,
it is possible, based on the speed of your processor, that the DATE$
function will give you the old date (just before midnight), and a
subsequent TIME$ function will give you the time for the new date (just
after midnight). In which case your date and time will be 24 hours off.

The following code will synchronize the date ann time.

'get today's date and time
dt$=DATE$
tm$=TIME$
if left$(tm$,2)="00" then dt$=DATE$

We first get the date and then the time. Then we check if the hour is 00
indicating that it's just past midnight. If so, we get the date again
to make certain that we have the new date corresponding to the new time.

Regards..... Moneo
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #10 on: October 18, 2007, 10:29:25 PM »

[\quote]

The following code for determining a leap year is wrong.

   IF X MOD 400 = 0 OR (X MOD 100 = 0 OR X MOD 4 = 0) THEN

The code should be:
  ' If the year is evenly divisible by 4 and not divisible
   ' by 100, or if the year is evenly divisible by 400, then
   ' it's a leap year:

   IF (X MOD 4 = 0 AND X MOD 100 <> 0) OR (X MOD 400 = 0) THEN

Regards..... Moneo
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #11 on: October 18, 2007, 11:15:23 PM »

My advice: forget all about displaying the time continually. Why do you want to do that anyway? To be fancy? It is WAY to hard.

As you can see from my demo, I had to save and restore the cursor position. Now you introduce color. So I would have to save and restore that, too.

And any program that tries to do two things at once runs into the problem that INPUT will freeze the process. In other words, when you have an INPUT command, no other process can run until the user answers the question and presses Enter. Sorry, that's the way QBasic works. So you have to avoid INPUT and instead write your own INPUT routine using INKEY$ (no easy task!!!).

You are too junior a programmer to do all of that. Just leave all the gimmicks out and concentrate on solving the user's problem. I would not even waste time on COLOR commands - a waste of time.

Mac
Logged
Smurfy
New Member

Posts: 11


« Reply #12 on: October 18, 2007, 11:40:59 PM »

Well thank you first to MAC you were a big help but the reason I need it in my program is because thats what was asked of me by a friend, I'm better at QB than he is and he needs it for some college course hes taking on programming and hes decided to pay me $100 for it and I need all the money I can get for college.

Second thank you Moneo, I totally overlooked that.
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #13 on: October 19, 2007, 10:29:17 AM »

pay me $100

Heh - What's my share?

Mac

DECLARE FUNCTION MyInput% (m$)
' Leapyear Calculator
ON TIMER(1) GOSUB UpdateTime
TIMER ON
DO
  CLS : LOCATE 3, 1
  TimeDisplayShowing = 0: WHILE TimeDisplayShowing = 0: WEND
  DO
    PRINT "Would you like to compute a range of years(1) or a single year(2): ";
    DO: q$ = INKEY$: LOOP WHILE q$ = ""
    IF q$ = CHR$(27) THEN SYSTEM
  LOOP WHILE q$ <> "1" AND q$ <> "2"
  PRINT q$
  IF VAL(q$) = 1 THEN GOSUB Range
  IF VAL(q$) = 2 THEN GOSUB SingleYear
  LOCATE 25, 1: PRINT "Would you like another run? Y/N: ";
  DO: R$ = UCASE$(INKEY$): LOOP WHILE R$ <> "Y" AND R$ <> "N"
LOOP WHILE R$ = "Y"
CLS
SYSTEM

Range:
DO
  F = MyInput("Enter the first year")
  L = MyInput("Enter the last year")
  IF F >= L THEN PRINT "Range out of order"
LOOP WHILE F >= L
FOR y = F TO L: GOSUB DoYear: NEXT y
RETURN

SingleYear:
y = MyInput("Enter a year")
GOSUB DoYear
RETURN

DoYear:
m = 2
IF y MOD 4 = 0 THEN
  m = 1
  IF y MOD 100 = 0 THEN
    m = 2
    IF y MOD 400 = 0 THEN m = 1
  END IF
END IF
IF m = 1 THEN
  TIMER OFF: COLOR 4, 0
  PRINT y; "is a leap year."
  COLOR 7, 0: TIMER ON
ELSE
  PRINT y; "is not a leap year."
END IF
RETURN

UpdateTime:
row = CSRLIN
col = POS(1)
LOCATE 1, 60: PRINT DATE$; " "; TIME$
LOCATE row, col, 1
TimeDisplayShowing = -1
RETURN

FUNCTION MyInput% (m$)
DO
  LOCATE , , 1: PRINT m$; ": ";
  start = LEN(m$) + 3
  w$ = ""
  DO
    DO: k$ = INKEY$: LOOP WHILE LEN(k$) <> 1
    SELECT CASE k$
    CASE "0" TO "9": IF LEN(w$) < 4 THEN w$ = w$ + k$: PRINT k$;
    CASE CHR$(13): PRINT
    CASE CHR$(27): SYSTEM
    CASE CHR$(8):
      LOCATE , start: PRINT SPACE$(4); : LOCATE , start
      IF LEN(w$) > 1 THEN
        w$ = LEFT$(w$, LEN(w$) - 1): PRINT w$;
      ELSE
        w$ = ""
      END IF
    END SELECT
  LOOP WHILE ASC(k$) <> 13
  PRINT
  IF VAL(w$) < 1583 THEN PRINT "Must be greater than 1582"
LOOP WHILE VAL(w$) < 1583
MyInput% = VAL(w$)
END FUNCTION

« Last Edit: October 19, 2007, 10:31:32 AM by Mac » Logged
Smurfy
New Member

Posts: 11


« Reply #14 on: October 19, 2007, 01:46:49 PM »

Thank you so much you have been the biggest help possible!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!