time struct using CLASSES

Hi,

I'm trying to create a program using nothing but classes as a homework assignment. (yes another help me on my homework post)

so far i've pulled this sample code off the web:

1
2
3
4
5
6
7
8
    struct tm *ptr;
    time_t ltime;
    char str[80];
    
    ltime = time(NULL); /* get current calendar time */
    ptr = localtime(&ltime);  // return time in the form of tm structure    
    strftime(str,80,"It is now %H:%M %p.",ptr);
    printf("%s\n",str);


i have so far set up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Time
{
	int hour, minute;
public:
	//Getters
	int GetHour() {return hour;}
	int GetMinute() {return minute;}

	//Setters
	void SetHour(int nhour)
	{
                // code i was working with previously... do loop N/A
		hour = nhour;
		do{
			cout << "Hour is: ";
			cin >> nhour;
		}while(nhour > 12);
	}
	void SetMinute(int nminute)
	{
                // code i was working with previously... do loop N/A
		minute = nminute;
		do{
			cout << "Minute is: ";
			cin >> nminute;
		}while(nminute > 59);
	}
	//Printers
	void PrintTime();
};

int main()
{
     Time timel;
     return 0;
}


I'm stuck at the part where i would want to reference the time into variables held by my class. Or perhaps have the time struct in my class somewhere and use my CLASS to access the time.

eventually my goal here is to do hour++ and minute++ when I can successfully get the time extracted into the variables hour, minute


any suggestions?
1
2
3
4
5
6
int main()
{
    Time time1;
    std::cout << time1.GetHour() << std::endl;
    return 0;
}

It might help if your Time class knew what time it was.
yes, you are correct. I didn't include all my code.

I do have that bit in my PrintTime() function.

My main concern was with the Time struct working as it should. How can I go about extracting the time into my time1.hour and time1.minute variables.
The tm struct has various pieces of time information within it that you can use to set your member variables.
Topic archived. No new replies allowed.