Howdy, I have a few questions regarding the use of ctime within a class.
I followed the example on http://www.cplusplus.com/reference/clibrary/ctime/strftime/

When the program is like this, it works:

#include <ctime>
#include <iostream>
using namespace std;

int main(){
	time_t current_seconds;
	current_seconds = time(NULL); //Fills current_seconds with the number of seconds since January 1, 1970
	struct tm * timeinfo;
	char buffer [80];
	time (&current_seconds);
	timeinfo = localtime (&current_seconds); 
	strftime (buffer,80,"Event Date: %x",timeinfo); //Modifies timeinfo into a user readable format
	puts(buffer); // Posts the string "buffer"
	strftime (buffer,80,"Event Time: %X %Z",timeinfo);
	puts(buffer);

return 0;
}

Question 1: Is it possible to place this in a class, such as class Event_Time:?

class Event_Time {
public: 
	time_t current_seconds;
	current_seconds = time(NULL); //Fills current_seconds with the number of seconds since January 1, 1970
	struct tm * timeinfo;
	char buffer [80];
	time (&current_seconds);
	timeinfo = localtime (&current_seconds); 
	strftime (buffer,80,"Event Date: %x",timeinfo); //Modifies timeinfo into a user readable format
	puts(buffer); // Posts the string "buffer"
	strftime (buffer,80,"Event Time: %X %Z",timeinfo);
	puts(buffer); 
		
};

However, when I paste the code in VS2010, everything shows up red, even though I've included <ctime> and I'm placing it before main. The majority of the errors read: "this declaration has no storage class or type specifier"

Question 2: can I assign a variable to the value of timeinfo?
Meaning event_time = timeinfo;
I attempted to do:

tm event_time;
event_time = localtime(&current_seconds);

but I get the warning/error "no operator matches these operands"

I think I figured at least a partial solution to number 2.
In 6 where buffer is defined, that can be changed to event_time - it's an array of type char.
It then can be used later with puts and cout <<

One big thing you have to understand is that a function (ie main()) is not a class.

Classes hold variables and functions which can be accessed/used via the "dot" (.) operator.

Here is an example of an Event_Time class that prints out the current date and time when you call the function CurrentTime().

#include <iostream>
#include <ctime>

using namespace std;

class Event_Time
{
	time_t current_seconds;
	tm *timeinfo;
	char buffer[80];

	public:

	void CurrentTime()
	{
		current_seconds = time(NULL);
		timeinfo = localtime(&current_seconds);
		strftime( buffer, 80, "Event Date: %x", timeinfo );
		cout << buffer << endl;
		strftime( buffer, 80, "Event Time: %X %Z", timeinfo );
		cout << buffer << endl;
	}
};

int main()
{
	Event_Time event; //creates a new event

	event.CurrentTime(); //displays the current time

	return 0;
}

Notice how there is no assignment done outside of functions within the class (if you want to declare and initialize at the same time outside of functions within the class then it must be a static constant variable (ie static const int x = 5 ).

If you want to initialize variables right when the object is made then you either call a function that assigns or you define a constructor for the class.

Here is an example using a constructor.

#include <iostream>

using namespace std;

class myClass
{
	int myVariable;


	public:

	myClass() //Constructor. Notice that it does not have a return type. This will replace the default constructor.
	{
		myVariable = 5;
	}

	myClass( int in ) //This overloads the constructor which allows you to have multiple constructors (both are able to be used).
	{
		myVariable = in;
	}

	void ShowNumber()
	{
		cout << myVariable << endl;
	}
};

int main()
{
	myClass aClass; //myVariable gets assigned the value of 5
	myClass anotherClass(10); //myVariable gets assigned the value of 10

	aClass.ShowNumber(); //outputs 5
	anotherClass.ShowNumber(); //outputs 10

	return 0;
}

There is lots of information on classes. The website you are using for examples has a section on classes.

Thanks for your help, I'll make sure to go over and review classes more, as well as functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.