Hello! First post so I'd like to say hello to everyone :)

So I've got this code here, I am very new to C++ and going through the basics, and I am creating a program that will tell the user when he/she is born. So the user puts his/her age in and I want to get the system year and just subtract it from the age, rather then having the user putting the year in.

The code I use is this:

#include <iostream>
#include <ctime>
using std::cout;
using std::endl;

int main()
{
    char date[9];
    _strdate(date);
    cout << date << endl;
}

and it outputs for example; 28/08/2009. But I have absolutely no idea how to filter it so that it only gets the year and then to subtract it from what the user puts in.

Comments
You're welcome :) BTW, very good first post (e.g. code tags on first post)!

Perhaps use windows.h if your OS is windows.

#include<iostream>
#include<windows.h>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	_SYSTEMTIME st;
	GetSystemTime(&st);

	cout<<st.wMonth<< "\\" << st.wDay <<"\\" << st.wYear<<endl;


}

or google

I'd just go with a few standard functions. Then the math of subtracting years is pretty simple.

#include <iostream>
#include <ctime>
using namespace std; 

void foo(int age)
{
   time_t now = time(0);
   if ( now != (time_t)-1 )
   {
      struct tm *local = localtime(&now);
      if ( local )
      {
         local->tm_year -= age;
         time_t then = mktime(local);
         if ( then != (time_t)-1 )
         {
            std::cout << ctime(&then);
         }
      }
   }
}

int main()
{
   foo(19);
   return 0;
}

/* my output
Tue Aug 28 14:56:49 1990
*/

Perhaps use windows.h if your OS is windows.

#include<iostream>
#include<windows.h>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	_SYSTEMTIME st;
	GetSystemTime(&st);

	cout<<st.wMonth<< "\\" << st.wDay <<"\\" << st.wYear<<endl;


}

or google

Thanks, I will try this out right away!

But don't you google me, I googl'd but couldn't find any good. I did find that page but I didn't understand it, and that's why I posted it on this forum

I'd just go with a few standard functions. Then the math of subtracting years is pretty simple.

#include <iostream>
#include <ctime>
using namespace std; 

void foo(int age)
{
   time_t now = time(0);
   if ( now != (time_t)-1 )
   {
      struct tm *local = localtime(&now);
      if ( local )
      {
         local->tm_year -= age;
         time_t then = mktime(local);
         if ( then != (time_t)-1 )
         {
            std::cout << ctime(&then);
         }
      }
   }
}

int main()
{
   foo(19);
   return 0;
}

/* my output
Tue Aug 28 14:56:49 1990
*/

Awesome code, works brilliant. But I need to learn what some of these things mean,

Why subtract 1 from time_t? (time_t)-1

What does the line struct tm *local = localtime(&now); actually do?

Answers are appreciated, I will google and look on sites. But if you have time!

I googl'd but couldn't find any good.

This indicates to me that you didn't have success with web searches.

Yeah, ha-ha. So funny huh :)

You don't think I actually googl'd but didn't understand it so I posted my post?

If you don't care for my help, fine.

This indicates to me that you didn't have success with web searches.


If you don't care for my help, fine.

If you define help by typing in like 10 letters in google things went wrong

If you define help by typing in like 10 letters in google things went wrong

You also have to click on one of the links :P
Only by clicking on the first, I get to this page, which already contains a bunch of brilliant information.

This article has been dead for over six months. Start a new discussion instead.