Thread: Date data type?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Date data type?

    Hi,

    I was wondering how I can store a date in my program.

    Basically the program will work like this:

    Code:
    cout << "Enter the date of the appointment";
    cin >> ????
    I would like to store the date the user enters using some type of 'date datatype'.

    I have found how to get the current date from the host system, but I'm wanting to store future dates.

    Hope someone can advise me on how to accomplish this simply.

    Many thanks!

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I would probably use a structure or class, so then you can have different variables such as month, day, and year and they would be contained in a sort of "data type" that you defined.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no built-in date type.
    What you would need to do is use the C struct tm or your own struct/class and overload operator >>.
    Otherwise, you can input into a string and parse it. But that is pretty much what you would have to do inside operator >> anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks guys. That's what I'll do then.

    Appreciated.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alternatively, there is boost. They have date classes. I do not know if they have operator >>, but you could always look. Boost is usually great.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep, Boost.DateTime has input/output operations. I'm not sure how flexible the parsing code is, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Replies: 0
    Last Post: 03-20-2008, 07:59 AM