Two date structures "sharing" the same date
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Two date structures "sharing" the same date

  1. #1
    Join Date
    Mar 2014

    Two date structures "sharing" the same date


    I'm a new C++ programmer (although I've done BV et al for years) so I may be missing something obvious here, but what is wrong with this?

    I have two date/time structures which I'm populating, but when I populate the second one it sets the same values in the first.

    This is what I've got so far

    tm *FirstDate = gmtime(&now);
    tm *SecondDate = gmtime(&now);

    cout <<"Enter your first date in the format dd/mm/yyyy" << endl <<">";
    getline (cin,tempstring);

    FirstDate->tm_mday = atoi(tempstring.substr(0,2).c_str());
    FirstDate->tm_mon = atoi(tempstring.substr(3,2).c_str());
    FirstDate->tm_year = atoi(tempstring.substr(6,4).c_str());

    // this is correct...
    cout<<" your first entered " <<FirstDate->tm_mday << "/" <<FirstDate->tm_mon <<"/" <<FirstDate->tm_year <<endl;
    cout <<"Now enter your second date in the format dd/mm/yyyy" << endl <<">";
    getline (cin,tempstring2);

    SecondDate->tm_mday = atoi(tempstring2.substr(0,2).c_str());
    SecondDate->tm_mon = atoi(tempstring2.substr(3,2).c_str());
    SecondDate->tm_year = atoi(tempstring2.substr(6,4).c_str());

    // at this point they're both the same as the last entry..
    cout<<" your first entered " <<FirstDate->tm_mday << "/" <<FirstDate->tm_mon <<"/" <<FirstDate->tm_year <<endl;
    cout<<" your second entered " <<SecondDate->tm_mday << "/" <<SecondDate->tm_mon <<"/" <<SecondDate->tm_year <<endl;

    What have I missed or misunderstood?



  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Two date structures "sharing" the same date

    When posting code, please format your code and use code tags. Go Advanced, select the code and click '#'.

    What have I missed or misunderstood?
    From MSDN

    The gmtime, mktime, and localtime functions use the same single, statically allocated structure to hold their results. Each call to one of these functions destroys the result of any previous call. If timer represents a date before midnight, January 1, 1970, gmtime returns NULL. There is no error return.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    Join Date
    Mar 2014

    Re: Two date structures "sharing" the same date

    Ahh, thanks 2kaud,

    Still got the VB mindset where you can do this sort of thing. I'll try another approach.

    Sorry about the code insert. I'll know next time


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a survey!

On-Demand Webinars (sponsored)