GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Feb-2009, 11:01
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Date and time


I am working on a program for a restaurant menu where the user can buy meals, drinks, and so forth. I have the basic code in place and want to add in a specials menu that changes daily. At this time I am using __DATE__ and __TIME__ to get the date and time.

My issue is that I would like the time to change as if it was a clock. So how would I manage this ?

Regards,
Gene Saika
  #2  
Old 05-Feb-2009, 12:32
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: date and time


Have a look at ctime
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 05-Feb-2009, 23:21
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: date and time


Just to be certain... calling ctime(t) would make it so that if the user were to go to a different part of the program and come back to where the clock is the time would be the time at that moment?

Also would the time be constantly refreshing or at least refreshable?
  #4  
Old 06-Feb-2009, 06:13
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: date and time


Yes. Look at the following example:
CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>

char * show_date () {
        time_t t = time (NULL);
        return ctime (&t);
}

int main(void) {
        printf ("Date at start   : %s", show_date());
        sleep (10);
        printf ("Date after sleep: %s", show_date());
        return 0;
}
And the output:
Quote:
Originally Posted by output
Date at start : Fri Feb 6 07:11:03 2009
Date after sleep: Fri Feb 6 07:11:13 2009
Each time you call the function you get the most current time. If you need something finer than second granularity you can look at gettimeofday.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #5  
Old 06-Feb-2009, 09:53
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: date and time


Thank you that should work perfectly.

Would you mind explaining to me the use of unistd.h I have never heard of that header file.
  #6  
Old 06-Feb-2009, 12:26
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: date and time


My system man pages (it is a Linux system) say that the required header for sleep is unistd.h
Some systems may work without it, but I have it there for completeness.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #7  
Old 06-Feb-2009, 17:19
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: date and time


ok thank you again
  #8  
Old 10-Feb-2009, 10:27
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: Date and time


After implamenting the code into my program i found that it does work.

Now I need this to refresh constantly and still display other things after that and get input from them.

This is how I have the code so far ( not the full code but an example)

CPP / C++ / C Code:
#include <iostream.h>
#include <stdio.h>
#include <time.h>

char * show_date () {
        time_t t = time (NULL);
        return ctime (&t);
}

int main()
{

int ct = 0 ;
int option = 0 ;

cout << " options 1 - 5" ;

while (ct == 0 ){
cout << show_time () ;
Sleep (1000) ;
}

cout << " Which option do you want?" ;
cin >> option ;

return 0 ;


I figure I could get the program to clear the line in the while loop and that would fix the repeating of the info, but I don't know how to do that.

Also I need the code to still ask the user which option they want and get the input.

I am using windows OS.

Regards,
Gene Saika
  #9  
Old 10-Feb-2009, 11:15
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Date and time


I'm not sure what you are trying to do here.
Are you looking to have the user input some value and then display the time that input was read? Or are you looking to continually output the time until a user makes a selection? If you put a display/sleep execution within a while loop and do not change the conditional to that loop (ct == 0 in your example) that is what is called an infinite loop.
You may have other intentions. Please clarify
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #10  
Old 10-Feb-2009, 17:38
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: Date and time


I am wanting the time to be like a clock that ran in the of my program while everything else still continues to move on.

So yes I want the time to be in an infinite loop so that is constantly refreshing, but I want the rest of the program to keep going.

So when run the program should have a digital clock running with the current time while still letting the user input and move about the program.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Get the date / time difference with PHP admin PHP Code Library 4 16-Nov-2006 00:43
Date and time formats from mysql jcgkiwi MySQL / PHP Forum 1 21-Jun-2005 09:37
Limit combo box and date time picker choice according to database created in folder shinyhui C++ Forum 0 22-Feb-2005 21:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 03:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.