Thread: How do I get the current date in my code?

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    How do I get the current date in my code?

    I was wondering how to get the current date(seconds/hours/days/years etc..) in my code. I just started learning c++ so I don't have any real experience at all. I would really like to know It becouse I am trying to finish my first (tiny) program, I need the program to display the current date.

    Just like this:

    #include <stdio.h>
    #include <iostream.h>

    int main()

    {
    int age;
    int year= // this is where I want the code to put the current date
    cout<<"Please enter your date of birth";

    cin>>age;

    cin.get();

    int (age=year-age);

    cout<<"You are "<<age; cout<<" years old";

    cin.get();

    return 0;

    }

    I know It looks kinda noob-like but I really need to know..
    You can post a reply or mail me at: [email protected]

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    :)

    You either need a user to enter the date, through cin >> date or through a date class. Why don't you just make is a const variable 2003?

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    
    {
    int age;
    const int year= 2003;
    cout << "Please enter your date of birth (Ex. 1918)" << endl;
    cin >> age;
    cout << endl << "You are " << (year-age) << " years old";
    return 0;
    }

  3. #3
    Anonymous Bob
    Guest

    Talking

    That last code segment posted by christee is wrong because it will say that someone (for ex: Bob) was born on December 1, 2002 is 1 year old on January 1, 2003 and he is really 1 month old.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    look for stritime orsomething like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. C/C++ Date manipulation code
    By lordofthering in forum C Programming
    Replies: 3
    Last Post: 04-10-2002, 03:49 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. FAQ: How do i display current time and date ?
    By davie_scotland in forum FAQ Board
    Replies: 2
    Last Post: 01-24-2002, 11:18 AM