Using the struct tm.

So I am trying to use the tm struct to get the minutes second and hours
I have tried the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <time.h>


using namespace std;

int main()
{
	struct tm *theTime;
	int hours = theTime.tm_hour;

	cout<<hours<<endl;
}


and I am getting the following error: request for member ‘tm_hour’ in ‘theTime’, which is of non-class type ‘tm*’

I dont really get why this does not work....If anyone could fix this that would be great... thanks :)

You declared theTime as a pointer, so you must use -> to access members instead of .

Anyway, there are other problems:
- Include ctime, not time.h for C++ programs
- Your struct pointer is never initialized, so it points to garbage
- You need to populate the struct; it isn't automatically set to the current time when created
#include <iostream>
#include <ctime>

1
2
3
4
5
6
7
8
9
10
11
12
using namespace std;

int main()
{
	struct tm *theTime;
	time_t tim;
	time(&tim);
	theTime = localtime(&tim);
	int hours = theTime->tm_hour;

	cout<<hours<<endl;
}


Try this
Thanks man, worked perfectly :)

Now I just gotta use those values for a bunch of stuff :DDDDD
Topic archived. No new replies allowed.