Time Class

closed account (365X92yv)
Trying to get this code to work. What the current issue I have is how do I get the now object to get the current time. I also want to know how to use the bool variable so that I can get it to say am or not. Any help with this is appreciated.

Thanks.

(Answers are not as valuable as successful teaching. Don't give me the answer. Help me understand what would make it work and how.)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// This lab exercise is to practice defining a class with methods.
***********************************************************************************

#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 

// ***********************************************************************************
// In this lab, you'll be defining a simple time class with a constructor
// and a method, in addition to data members.
//
// ***********************************************************************************

// Part A.
// declare a class called Time with
// 1) two int data members called hour and min
// 2) a boolean data member called am
// 3) a constructor that takes no arguments
//    (remember that a constructor has the same name as the class, and no return value)
// 4) a Print method that takes no arguments and returns void

// DON'T FORGET THAT YOUR CLASS DEFINITION ENDS WITH ;

class Time
{
public:
	int hour, min;
	bool am;
	Time();
	void Print();
};

// Part B.
// define the two methods you declared in your class
// remember that their full name are: Time::Time and Time::Print

// 1) Time::Time should set hour and min to 0 and am to true

Time::Time()
{
	hour = 0;
	min = 0;
	am = true;
}

void Time::Print()
{
	cout << hour << ":";
	cout << setw(2) << setfill( '0' ) << min;
}



// Part C.
// for contrast, declare a global function print that takes a Time
//  object and prints it, returning nothing

void print(Time t1)
{
	cout << t1.hour << ":" << t1.min;
}



int main( )
{	
	// Part D.
	// 1) declare a Time object called now
	// 2) set the hour, min and am members to the current time
	// 3) call the class print method to print the time
	// 4) call the global print function to print it again
	Time now;

	now.Print();
	print(now);
		
	return 0; 
}
You can use <ctime> to fetch the number of seconds that have passed since the 1st of January, 1970, and deduce the time of day from that.

Take a look at the ctime class to see what else is available to you - http://www.cplusplus.com/reference/clibrary/ctime/

closed account (365X92yv)
So is there no way I can use some sort of function to read what my computer clock says and manipulate that into my time object? Seems like a bit of work trying to count down to what time it is exactly from what looks like an 11 digit number.
Yes, you can use the sort of functions that are provided in <ctime> to read what the computer clock says and manipulate that into your time object.
Use time() then use localtime() and store a tm struct inside your object, and then use strftime() to convert it to a string in a standardized way.
closed account (365X92yv)
I tried using the time libraries but it seems to have some bugs that aren't expected. I figured I'd just take the time input from the user. It seems to work flawlessly like that. Thanks for the help. If anyone can figure out in their own time how to get the code to take the seconds and get the current time of day off of that please post or pm me so I can understand it.
Don't give me the answer.

Well that didn't last long....
If anyone can figure out in their own time how to get the code to take the seconds and get the current time of day off of that please post or pm me so I can understand it.



I tried using the time libraries but it seems to have some bugs that aren't expected.

I will bet dollars to doughnuts - hell, I'll bet my life savings against a lollipop - that the bug is not in the libraries, but in your code.

I assume you found the following code where I directed you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  
  return 0;
}



It's easily modified as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* localtime example */
#include <cstdio>
#include <ctime>
#include<iostream>

using namespace std;
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  
  int hours = timeinfo->tm_hour;
  int minutes = timeinfo->tm_min;
  cout << "Hours: " << hours << " Mins: " << minutes << endl;
 
  return 0;
}


and there we have it - the hour and the minute.
Last edited on
closed account (365X92yv)
Can you explain what all this code does?

1
2
3
4
5
6
7
8
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  
  int hours = timeinfo->tm_hour;
  int minutes = timeinfo->tm_min;


I'd like to understand what the jargon is and why its used.

Also, we never went over structs in my CS class so I have no idea how they work and also pointers is next tuesday's lesson.
Last edited on
time_t is a integral type which represents seconds since midnight Jan 1 1970.
C structs like struct tm are like classes but they have no member functions or inheritance, only public members.
We call time to set rawtime to the current time.
We call localtime to get a struct tm representing the current time. It contains various members representing parts of the date.
Topic archived. No new replies allowed.