Function prototypes

Is it possible to shorten the following code into a function prototype ( ??or class?? ),
If so, How would I go about doing that...

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
#include <iostream>
#include <ctime>
using namespace std;

int main() 
{ 

/*      SETTING TIME AS  INTEGERS      */
 
     time_t rawtime;
     struct tm * timeinfo;
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
     M1 = timeinfo->tm_mon;
     Y1 = timeinfo->tm_year;
     D1 = timeinfo->tm_mday;
     MN1 = timeinfo->tm_min;
     S1 = timeinfo->tm_sec;
     H1 = timeinfo->tm_hour; 
     Y2 = Y2 + 1900;
     M2 = M2 + 1;
/////////////////////////////////////////

cout << "Day: " << D1 << " Month: " << M2 << " Year: " << Y2 << endl; //ECT.
cin.get();
return 0;
}


Im not trying to print the date, I am trying to get it to return the date as variables....
I just don't know how to go about doing this...
Last edited on
Do you know what a function prototype is? Anyway, here's the basics: http://www.cplusplus.com/doc/tutorial/functions/
I despise function objects. They're just so ridiculous. Redefining the call operator is a violation of the call operator's original purpose - calling a function. (That's just my opinion.) But you could theoretically accomplish this with a special time wrapper class of your own. I'd prefer a function but you could set up a constructor to generate a time wrapper of the current time (however, if you don't know how to implement a function I'd hold off on classes for now).
Do you know what a function prototype is?

Yes...
Bad example, I know. But I want this to be in a class, and ONLY one class

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
#include <iostream>
#include <ctime>

using namespace std;

int y ()
{
    
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int Y = timeinfo->tm_year;
    int Y2 = Y + 1900;
    return (Y2);
}

int m ()

{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int M = timeinfo->tm_mon;
    int M2 = M + 1;
    return (M2);
}

int d ()
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int D = timeinfo->tm_mday;
    return (D);
}

int h () 
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int H = timeinfo->tm_hour;
    return (H); 
}

int mn () 
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int MN = timeinfo->tm_min;
    return (MN);
}

int s ()
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    int S = timeinfo->tm_sec;
    return (S);
}

int main () {
    
    cout << d() << "/" << m() << "/" << y() << endl;
    cout << h() << ":" << mn() << ":" << s() << endl;
    cin.get();
    return 0;
}
Last edited on
I am not sure If I am understanding you correctly...

But any way...
I'd prefer a function but you could set up a constructor to generate a time wrapper of the current time



Again... I am not trying to print the current time...

I am trying to set the current time as a variable using a class or a function prototype ( I would prefer a class)
I would like to do this using only one class or Funct. Prototype.

All I am asking is for some guidance.

thank you
Last edited on
so to get you right:

you want to pack your data into a class and return that class from a function, so the caller of that function can get the data (for example day and hour) out of the class?...

after i read again, its a YES...

so put ur variables (like M1, Y1 etc) into a class as members and the code
1
2
3
4
5
6
7
8
9
10
11
12
time_t rawtime;
     struct tm * timeinfo;
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
     M1 = timeinfo->tm_mon;
     Y1 = timeinfo->tm_year;
     D1 = timeinfo->tm_mday;
     MN1 = timeinfo->tm_min;
     S1 = timeinfo->tm_sec;
     H1 = timeinfo->tm_hour; 
     Y2 = Y2 + 1900;
     M2 = M2 + 1;


into your constructor or an member function - if u want it to be set manually...
if you specify the member variables as public you can access them directly by using object.member;

or you could use data hiding and set them to private or protected and get them returned from the individual member functions... like unsigned short GetMonth(){return M1;};
Last edited on
Something like this is a start...

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

class Time
{
private:
    time_t unixtime;
    struct tm* timeinfo;
    int year, month,day, hour, minute, second;
public:
    Time()
    : unixtime(time(0)), timeinfo(localtime(&unixtime)),
      year(timeinfo->tm_year), month(timeinfo->tm_mon),
      day(timeinfo->tm_mday), hour(timeinfo->tm_hour),
      minute(timeinfo->tm_min), second(timeinfo->tm_sec)
    {}
    int getYear() const { return year; }
    // ...
};
whats the unix time all about
im trying to use rawtime
Last edited on

Now how would I call the individual functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <ctime>
class Time
{
private:
    time_t rawtime;
    struct tm * timeinfo;
    int year, month, day, hour, minute, second;
public:
    Time()
    : rawtime(time(0)), timeinfo(localtime(&rawtime)),
      year(timeinfo->tm_year), month(timeinfo->tm_mon),
      day(timeinfo->tm_mday), hour(timeinfo->tm_hour),
      minute(timeinfo->tm_min), second(timeinfo->tm_sec)
        {
    int Y() const { return year + 1900; }
    int M() const { return month + 1;}
    int D() const { return day; } 
    int H() const { return hour; }
    int MN() const { return minute; }
    int S() const { return second; }
        };


does anybody spot errors
1
2
3
Time t;

cout << "The date is: " << t.Y() << "-" << t.M() << "-" << t.D() << endl;
1
2
3
Time t;

cout << "The date is: " << t.Y() << "-" << t.M() << "-" << t.D() << endl;


Where does the Time t; go...

and can't Time t; be Time::t


And the file isn't compiling right...

Errors being in the class...
Last edited on
'Time' is a class. By doing Time t; you create an object of that class (called t). This is just like making a variable with int foo; or string bar;, but instead of making an int or a string, you're making a 'Time' object.

This time object takes a "snapshot" of the current time when you create it. That time can be read back through the various functions.

Where does the Time t; go...

Wherever you want the snapshot of the current time.

and can't Time t; be Time::t


No. Time t; creates an object as previously explained.

Time::t accesses 't' which is a member of the Time class. But you'll notice that Time doesn't have any member named 't', so it's nonsense.
Last edited on
And the file isn't compiling right...

Errors being in the class...


You whacked off the closing brace of the (empty) constructor body.

Your line 14 should look exactly like my line 15.
This won't compile...

I have no idea what is wrong with it...


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
#include <iostream>
#include <ctime>

using namespace std;

class Time
{
private:
    time_t rawtime;
    struct tm * timeinfo;
    int year, month, day, hour, minute, second;
public:
    Time()
    : rawtime(time(0)), timeinfo(localtime(&rawtime)),
      year(timeinfo->tm_year), month(timeinfo->tm_mon),
      day(timeinfo->tm_mday), hour(timeinfo->tm_hour),
      minute(timeinfo->tm_min), second(timeinfo->tm_sec)
        {
    int Y() const { return year + 1900; }
    int M() const { return month + 1;}
    int D() const { return day; } 
    int H() const { return hour; }
    int MN() const { return minute; }
    int S() const { return second; }
        };

int main ()
{
    Time t;

    cout << "The date is: " << t.Y() << "-" << t.M() << "-" << t.D() << endl;
            
    cin.get();
    return 0;    
}
Re-read my last post. Your change has now migrated to line 18.

And line 25 should have that closing brace flush to the left.
fixed
Cool. I hope you feel that helped you understand classes a bit. I know it can be a bit frustrating at times.

beginner at programming wrote:
whats the unix time all about
im trying to use rawtime


This will explain why I renamed your rawtime to unixtime: http://www.cplusplus.com/reference/clibrary/ctime/time_t/

Topic archived. No new replies allowed.