Working with dates

Discussion in 'C++' started by James Strickland, Nov 30, 2012.

  1. I'm working on a project for a class I'm taking.

    I'm trying take a date from a user, I want to calculate how many days away that is, from todays date (date of entry), then identify based on the number of days which one of 2 programs the user will fall under.

    http://pastebin.com/SxkjKW6j

    Currently, I'm taking in Month, day, year separately. I would like to just take in a date (i.e. 10/31/2012), then go from there.

    Right now, I'm Completely lost. Any help would be great!

    James
    James Strickland, Nov 30, 2012
    #1
    1. Advertising

  2. On 11/30/2012 10:30 AM, James Strickland wrote:
    > I'm working on a project for a class I'm taking.
    >
    > I'm trying take a date from a user, I want to calculate how many days
    > away that is, from todays date (date of entry), then identify based
    > on the number of days which one of 2 programs the user will fall
    > under.
    >
    > http://pastebin.com/SxkjKW6j
    >
    > Currently, I'm taking in Month, day, year separately. I would like
    > to just take in a date (i.e. 10/31/2012), then go from there.
    >
    > Right now, I'm Completely lost. Any help would be great!


    Not sure what "Completely lost" means. Do elaborate on that. And post
    your code. We're not going to click on some obscure link.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #2
    1. Advertising

  3. On Friday, 30 November 2012 09:37:45 UTC-6, Victor Bazarov wrote:
    > On 11/30/2012 10:30 AM, James Strickland wrote:
    >
    > > I'm working on a project for a class I'm taking.

    >
    > >

    >
    > > I'm trying take a date from a user, I want to calculate how many days

    >
    > > away that is, from todays date (date of entry), then identify based

    >
    > > on the number of days which one of 2 programs the user will fall

    >
    > > under.

    >
    > >

    >
    > > http://pastebin.com/SxkjKW6j

    >
    > >

    >
    > > Currently, I'm taking in Month, day, year separately. I would like

    >
    > > to just take in a date (i.e. 10/31/2012), then go from there.

    >
    > >

    >
    > > Right now, I'm Completely lost. Any help would be great!

    >
    >
    >
    > Not sure what "Completely lost" means. Do elaborate on that. And post
    >
    > your code. We're not going to click on some obscure link.
    >
    >
    >
    > V
    >
    > --
    >
    > I do not respond to top-posted replies, please don't ask


    Ok, sorry about that. I'm used to using pastebin. I'm lost as to how to input a date without separating month, day, year. I want to just input datein one line, (i.e. 12/31/2012). Then I want to calculate the number of days from the date entered to the date of entry (today).

    <code>
    #include <iostream>
    #include <ctime>

    using namespace std;

    int main()
    {
    int month;
    int day;
    int year;

    string monthName[12] = {"January", "February","March","April","May","June","July","August","September","October","November","December"};

    cout << "Enter the official Separation date" << endl;
    cout << "below. NOT YOUR TERMINAL LEAVE DATE!" << endl;
    cout << "Enter Month [1-12]: ";
    cin >> month;

    if (month > 12 || month < 1)
    { month = 1; }

    cout << "Enter Day [1-31]: ";
    cin >> day;

    if ( day > 31 || day < 1)
    { day = 1; }

    cout << "Enter Year [2012]: ";
    cin >> year;

    if ( year > 2015 || year < 2001 )
    { year = 2001; }


    system("pause");
    }
    </code>
    James Strickland, Nov 30, 2012
    #3
  4. On 11/30/2012 10:54 AM, James Strickland wrote:
    > [..] I'm lost as to how to input a date without separating month,
    > day, year. I want to just input date in one line, (i.e. 12/31/2012).
    > Then I want to calculate the number of days from the date entered to
    > the date of entry (today). [..]


    The easiest way to input a line would be to use 'std::getline' function.
    Once the you got the line, parse it by using any means available to
    you, one of which might be 'std::istringstream'. Check error state of
    the 'cin' and other streams often.

    (this is without testing)

    std::string input;
    if (std::getline(cin, input))
    {
    std::istringstream toparse(input);
    int month;
    if (toparse >> month)
    {
    if (month >= 1 && month <= 12) // and so on...

    Parsing input is not always simple. I am not sure what the best book is
    on the subject, so start by googling for "parse standard input C++" or
    some such.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #4
  5. On Friday, 30 November 2012 12:01:57 UTC-6, Christian Gollwitzer wrote:
    > Am 30.11.12 17:06, schrieb Victor Bazarov:
    >
    > > On 11/30/2012 10:54 AM, James Strickland wrote:

    >
    > >> [..] I'm lost as to how to input a date without separating month,

    >
    > >> day, year. I want to just input date in one line, (i.e. 12/31/2012).

    >
    > > > Then I want to calculate the number of days from the date entered to

    >
    > > > the date of entry (today). [..]

    >
    > >

    >
    > > The easiest way to input a line would be to use 'std::getline' function.

    >
    > > Once the you got the line, parse it by using any means available to

    >
    > > you, one of which might be 'std::istringstream'. Check error state of

    >
    > > the 'cin' and other streams often.

    >
    > > ....

    >
    > > Parsing input is not always simple. I am not sure what the best book is

    >
    > > on the subject, so start by googling for "parse standard input C++"

    >
    >
    >
    > That's one of the things I'm really disappointed with iostreams. Such a
    >
    > simple parsing problem could be solved by scanf using a pattern like
    >
    >
    >
    > %2d/%2d/%4d
    >
    >
    >
    > There is no equivalent in iostreams to do this in an easy way. The
    >
    > closest thing would be regexp, but that seems to be overkill.
    >
    >
    >
    > Christian


    Is there a way to do this simply buy subtracting two dates like you would in Excel?

    10/31/2013 - 10/31/2012 = 365 days?
    James Strickland, Nov 30, 2012
    #5
  6. On 11/30/2012 1:01 PM, Christian Gollwitzer wrote:
    > Am 30.11.12 17:06, schrieb Victor Bazarov:
    >> On 11/30/2012 10:54 AM, James Strickland wrote:
    >>> [..] I'm lost as to how to input a date without separating month,
    >>> day, year. I want to just input date in one line, (i.e. 12/31/2012).
    >> > Then I want to calculate the number of days from the date entered to
    >> > the date of entry (today). [..]

    >>
    >> The easiest way to input a line would be to use 'std::getline' function.
    >> Once the you got the line, parse it by using any means available to
    >> you, one of which might be 'std::istringstream'. Check error state of
    >> the 'cin' and other streams often.
    >> ....
    >> Parsing input is not always simple. I am not sure what the best book is
    >> on the subject, so start by googling for "parse standard input C++"

    >
    > That's one of the things I'm really disappointed with iostreams. Such a
    > simple parsing problem could be solved by scanf using a pattern like
    >
    > %2d/%2d/%4d
    >
    > There is no equivalent in iostreams to do this in an easy way. The
    > closest thing would be regexp, but that seems to be overkill.


    If sscanf serves all your needs, then use it. It's part of C++ Standard
    library. You don't have to use streams, unless your instructor insists
    that you do.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #6
  7. James Strickland

    Cholo Lennon Guest

    On 30/11/2012 15:08, James Strickland wrote:
    > On Friday, 30 November 2012 12:01:57 UTC-6, Christian Gollwitzer wrote:
    >> Am 30.11.12 17:06, schrieb Victor Bazarov:
    >>
    >>> On 11/30/2012 10:54 AM, James Strickland wrote:

    >>
    >>>> [..] I'm lost as to how to input a date without separating month,

    >>
    >>>> day, year. I want to just input date in one line, (i.e. 12/31/2012).

    >>
    >>> > Then I want to calculate the number of days from the date entered to

    >>
    >>> > the date of entry (today). [..]

    >>
    >>>

    >>
    >>> The easiest way to input a line would be to use 'std::getline' function.

    >>
    >>> Once the you got the line, parse it by using any means available to

    >>
    >>> you, one of which might be 'std::istringstream'. Check error state of

    >>
    >>> the 'cin' and other streams often.

    >>
    >>> ....

    >>
    >>> Parsing input is not always simple. I am not sure what the best book is

    >>
    >>> on the subject, so start by googling for "parse standard input C++"

    >>
    >>
    >>
    >> That's one of the things I'm really disappointed with iostreams. Such a
    >>
    >> simple parsing problem could be solved by scanf using a pattern like
    >>
    >>
    >>
    >> %2d/%2d/%4d
    >>
    >>
    >>
    >> There is no equivalent in iostreams to do this in an easy way. The
    >>
    >> closest thing would be regexp, but that seems to be overkill.
    >>
    >>
    >>
    >> Christian

    >
    > Is there a way to do this simply buy subtracting two dates like you would in Excel?
    >
    > 10/31/2013 - 10/31/2012 = 365 days?
    >


    In my case at least, boost usage is a simple way to do it, so I would
    use Boost.Date_Time library

    Regards

    --
    Cholo Lennon
    Bs.As.
    ARG
    Cholo Lennon, Nov 30, 2012
    #7
  8. On Friday, November 30, 2012 10:30:07 AM UTC-5, James Strickland wrote:
    > I'm working on a project for a class I'm taking.
    >
    >
    >
    > I'm trying take a date from a user, I want to calculate how many days away that is, from todays date (date of entry), then identify based on the number of days which one of 2 programs the user will fall under.


    I doubt this is what your instructor is looking for. However if you have aC++11 compiler/library available, here is how you can do it. It uses the new get_time and put_time manipulators in <iomanip> for the date I/O, specifying "%m/%d/%Y" for the input format which appears to be your preferred format. Then it uses <chrono> do convert a double number of seconds into an int number of days.

    #include <iostream>
    #include <iomanip>
    #include <chrono>

    template <class To, class Rep, class Period>
    To
    round_up(const std::chrono::duration<Rep, Period>& d)
    {
    To t = std::chrono::duration_cast<To>(d);
    if (t < d)
    ++t;
    return t;
    }

    int main()
    {
    typedef std::chrono::duration<int, std::ratio<86400>> days;
    typedef std::chrono::duration<double> dsecs;
    // Input separation date
    std::cout << "Enter separation date [mm/dd/yyyy]:";
    std::tm sep_tm = {0};
    while (true)
    {
    std::cin >> std::get_time(&sep_tm, "%m/%d/%Y");
    if (std::cin)
    break;
    std::cout << "\nInvalid format. Please enter separation date using[mm/dd/yyyy]:";
    sep_tm = std::tm{0};
    std::cin.clear();
    std::cin.ignore(1, '\n');
    }
    // show off a little
    std::cout << "You entered a separation date of " << std::put_time(&sep_tm, "%A, %b. %e, %Y\n");
    // get difference from current time in double seconds
    double secs = std::difftime(std::mktime(&sep_tm), std::time(nullptr));
    // convert double seconds to int days
    days d = round_up<days>(dsecs(secs));
    // Print it out
    switch (d.count())
    {
    case 0:
    std::cout << "That is today.\n";
    break;
    case -1:
    std::cout << "That was yesterday.\n";
    break;
    case 1:
    std::cout << "That is tomorrow.\n";
    break;
    default:
    if (d < days(0))
    std::cout << "That was " << d.count() << " days ago.\n";
    else
    std::cout << "That is in " << d.count() << " days.\n";
    break;
    }
    }
    Howard Hinnant, Dec 2, 2012
    #8
  9. On Friday, 30 November 2012 09:30:07 UTC-6, James Strickland wrote:
    > I'm working on a project for a class I'm taking.
    >
    >
    >
    > I'm trying take a date from a user, I want to calculate how many days away that is, from todays date (date of entry), then identify based on the number of days which one of 2 programs the user will fall under.
    >
    >
    >
    > http://pastebin.com/SxkjKW6j
    >
    >
    >
    > Currently, I'm taking in Month, day, year separately. I would like to just take in a date (i.e. 10/31/2012), then go from there.
    >
    >
    >
    > Right now, I'm Completely lost. Any help would be great!
    >
    >
    >
    > James


    sep_tm = std::tm{0};

    This returned the following error:
    1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
    1>Build started 12/5/2012 3:18:31 PM.
    1>InitializeBuildStatus:
    1> Touching "Debug\Project2.unsuccessfulbuild".
    1>ClCompile:
    1> Source.cpp
    1>c:\users\james\documents\visual studio 2012\projects\project2\project2\source.cpp(28): error C2275: 'tm' : illegal use of this type as an expression
    1>c:\users\james\documents\visual studio 2012\projects\project2\project2\source.cpp(28): error C2143: syntax error : missing ';' before '{'
    1>c:\users\james\documents\visual studio 2012\projects\project2\project2\source.cpp(28): error C2143: syntax error : missing ';' before '}'
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:01.20
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I'm using Visual Studio 2012.
    James Strickland, Dec 5, 2012
    #9
  10. On 12/5/2012 4:22 PM, James Strickland wrote:
    >[..]
    > sep_tm = std::tm{0};
    >
    > This returned the following error:

    [..]
    > I'm using Visual Studio 2012.


    VC++ 2012 has no support yet for the "brace-init-list" (8.5.4).

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 5, 2012
    #10
  11. On Wednesday, 5 December 2012 15:31:55 UTC-6, Victor Bazarov wrote:
    > On 12/5/2012 4:22 PM, James Strickland wrote:
    >
    > >[..]

    >
    > > sep_tm = std::tm{0};

    >
    > >

    >
    > > This returned the following error:

    >
    > [..]
    >
    > > I'm using Visual Studio 2012.

    >
    >
    >
    > VC++ 2012 has no support yet for the "brace-init-list" (8.5.4).
    >
    >
    >
    > V
    >
    > --
    >
    > I do not respond to top-posted replies, please don't ask


    WHAT? A microsoft product not supporting something? Thanks for the help, I'll have t compile it a different way. What can I use?

    -James
    James Strickland, Dec 5, 2012
    #11
  12. On 12/5/2012 6:29 PM, James Strickland wrote:
    > On Wednesday, 5 December 2012 15:31:55 UTC-6, Victor Bazarov wrote:
    >> On 12/5/2012 4:22 PM, James Strickland wrote:
    >>
    >>> [..]

    >>
    >>> sep_tm = std::tm{0};

    >>
    >>>

    >>
    >>> This returned the following error:

    >>
    >> [..]
    >>
    >>> I'm using Visual Studio 2012.

    >>
    >>
    >>
    >> VC++ 2012 has no support yet for the "brace-init-list" (8.5.4).
    >>
    >>
    >>
    >> V
    >>
    >> --
    >>
    >> I do not respond to top-posted replies, please don't ask

    >
    > WHAT? A microsoft product not supporting something? Thanks for the help, I'll have t compile it a different way. What can I use?
    >
    > -James
    >


    You could try "std::tm()" syntax instead.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 6, 2012
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    859
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    192
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    205
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    195
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    159
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page