struct tm

Jan 9, 2011 at 11:40pm
I've tried this:

1
2
3
4
5
6
7
8
9
void printings () {
        struct tm {
                int tm_mon;
                int tm_mday;
                int tm_year;
        };
        cout << endl << endl << title << endl << "Held on " << (tm_mon + 1) << "/" << tm_mday;
        cout << endl << (tm_year+1900) << location;
}


But it just says that t_mon, t_mday, and t_year are not declared in the scope.
Jan 9, 2011 at 11:58pm
well it's unusual to declare a struct inside of a function, if you're only using it inside of that function, then why use it at all? Just use 3 ints, and if you're using several instances of the struct, don't declare it inside of that function put it elsewhere where everything that needs it can see the definition.

But if you are going to declare it in the function, youll need to create an instance of the struct not just declare it, and then also initialise the values in the struct.

1
2
3
4
5
6
7
8
9
10
11
12
struct tm {
                int tm_mon;
                int tm_mday;
                int tm_year;
        }thisIsAStruct;

        thisIsAStruct.tm_mon = 1;
        thisIsAStruct.tm_mday = 2;
        thisIsAStruct.tm_year = 3;

        cout << endl << endl << title << endl << "Held on " << (thisIsAStruct.tm_mon + 1) << "/" << thisIsAStruct.tm_mday;
        cout << endl << (thisIsAStruct.tm_year+1900) << location;


location and title also weren't in the code you posted, I assume they're in your code but you didn't include them. This is pretty smelly code, I suspect you've not properly grasped what you're trying to do. What exactly are you making?
Last edited on Jan 9, 2011 at 11:59pm
Jan 10, 2011 at 12:08am
I'm making a script that automatically does minutes of a meeting, and I don't want the user have to input the time. I didn't really understand how the tm_ stuff worked; I don't get the struct.
Jan 10, 2011 at 12:16am
1
2
3
4
5
  struct tm {
                int tm_mon;
                int tm_mday;
                int tm_year;
        };


This code, from your original posting, essentially announces to the compiler that there's this new kind of object. It's called a tm, and it's a struct. It has inside it these three integers.

That's all you've done. You haven't actually made one; you've just explained how to make one.

Recall how you make an integer object:

int aNewIntegerObject;

You write the type, and then the name you want to give the new object.

tm aNewInstanceOfTheStructure;

Now, you have an object of type tm, which is the structure, and it's called aNewInstanceOfTheStructure

Then, to access the internals of the structure, you would enter

aNewInstanceOfTheStructure.tm_mon = 7;

and now the tm_mon field of your newly created object has the value 7.

Jan 10, 2011 at 11:45am
Why are you all doing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <ctime>
using namespace std;

int main()
  {
  time_t t = time( NULL );
  struct tm today = *localtime( &t );

  cout << "Today is " << (today.tm_mon + 1) << "/" << today.tm_mday << "/" << (today.tm_year+1900) << endl;

  return 0;
  }

If you looked up the struct tm you should have been able to look up the time() and localtime() functions, and examples of how to use them.

[edit]fixed omission...
Last edited on Jan 10, 2011 at 11:47am
Topic archived. No new replies allowed.