What am I doing wrong with struct tm?

This is a discussion on What am I doing wrong with struct tm? within the C++ Programming forums, part of the General Programming Boards category; I have look around and around, and now matter what I seem to do I just can't seem to get ...

  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
    32,853
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Since today is a pointer, it should be today->tm_mon.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:07 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 03: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21