Thread: What am I doing wrong with struct tm?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Question What am I doing wrong with struct tm?

    I have look around and around, and now matter what I seem to do I just can't seem to get this to work. I don't understand what I'm doing wrong. I've tried it with 'struct' in front of 'tm *today' as well

    When I debug it, I get "Error 2 error C2228: left of '.tm_mon' must have class/struct/union". Which I don't get since I thought I had declared it.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main ()
    {
    
    	char test;
    	time_t now;
    	tm *today;
    
    	time(&now);
    	today=localtime(&now);
    
    	cout << today.tm_mon << endl;
    
    	cout << "Did it work?";
    	cin >> test;
    
      return 0;
    }
    What am I doing wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's a pointer to a struct.

    So it's
    today->tm_mon;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since today is a pointer, it should be today->tm_mon.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Lightbulb Thanks

    Thanks, that was a silly thing to miss when I read that several times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread