Question about Time & Date

I am a C++beginner and I got a problem when dealing with time&date.
I always get a error that says 'tm' does not refer to a value when I try to call the function DigitalTime? How can I change the current code to fix the problem. Thanks guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ctime>

void DigitalTime(tm *localt)
{
        printf("Time: %d",localt->tm_hour);
        printf(": %d",localt->tm_min);
        printf(": %d\n",localt->tm_sec);
}

int main()
{
        time_t currenttime=time(0);  // current date/time based on current system
        tm *localt=localtime(&currenttime);  // print various components of tm structure.
        DigitalTime(tm *localt); // error:'tm' does not refer to a value
        return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <ctime>

void DigitalTime()
{
time_t currenttime=time(0); // current date/time based on current system
tm *localt=localtime(&currenttime); // print various components of tm structure.
printf("Time: %d",localt->tm_hour);
printf(": %d",localt->tm_min);
printf(": %d\n",localt->tm_sec);
}

int main()
{
DigitalTime();
}
Man, I know this would work. But what if there are multiple functions are gonna use localt->tm_sec,localt->tm_min and localt->tm_hour? If I put multiple time_t currenttime=time(0); and tm *localt=localtime(&currenttime); in the program, would they conflict with one another ?
Line 15; Your call to DigitalTime is incorrect. Remove the tm *

 
    DigitalTime (localt);


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Thanks.Problem solved.
If you want multiple functions to use the same time then what you wrote would be the way to go, however, if you want each function to use the current time, you should make each one get the time fresh.


For example a function that writes to disk is going to take a lot longer to run than one that doesn't. Any function running after the disk write could be off by several seconds.
Topic archived. No new replies allowed.