# Thread: C++ Question

1. This "human class" can be set to any birth date and it converts the date into timestamp
Code:
#define MONTH false
#define DAY true
#include <ctime>
#include <iostream>

class Human{
public:
int DayMonthToDay(int day,int month){
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31},count=0;
if((getAgeInfo('Y')-1970)%4==0){
days[1]=29;
}
for(int i=1;i<month%13;i++){
count+=days[i-1];
}
count+=day;
return count;
}
int DateDataFromDay(int day,bool dayt){
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31},i;
if((getAgeInfo('Y')-1970)%4==0){
days[1]=29;
}
for(i=0;day>days[i];i++){
day-=days[i];
}
if(dayt){ return day; }
return i+1;
}
void setBirthTime(int year,int month,int day,int hour,int minute,int second){
setAgeInfo('y',year);
setAgeInfo('d',DayMonthToDay(day,month));
setAgeInfo('h',hour);
setAgeInfo('m',minute);
setAgeInfo('s',second);
}
void setAgeInfo(char type,int tmpstamp){
int temporary;
switch(type){
//set the day of birth
case 'd':
if(tmpstamp<=366){
TimeStamp-=getAgeInfo('D')*3600*24;
TimeStamp+=tmpstamp*3600*24/*+((getAgeInfo('Y')-1970)/4)*3600*24*/;
}
return;
//set the hour of birth
case 'h':
TimeStamp-=getAgeInfo('H')*3600;
TimeStamp+=(tmpstamp%24)*3600;
return;
//set the minute of birth
case 'm':
TimeStamp-=getAgeInfo('H')*60;
TimeStamp+=(tmpstamp%60)*60;
return;
//set the total seconds of birth (since 1970)
case 'r':
TimeStamp=tmpstamp;
return;
//set the seconds of birth
case 's':
TimeStamp-=getAgeInfo('H');
TimeStamp+=(tmpstamp%60);
return;
//set the year of birth
case 'y':
temporary=(getAgeInfo('Y')-1970)*(3600*24*365);
TimeStamp-=(getAgeInfo('Y')-1970)*(3600*24*365)+((getAgeInfo('Y')-1970)/4)*3600*24;
TimeStamp+=(tmpstamp-1970)*3600*24*365+((tmpstamp-1970)/4)*3600*24;
return;
}
}
int getAgeInfo(char type){
int tmpint;
switch(type){
//get the day of birth
case 'D':
return (TimeStamp/(3600*24)%365)-TimeStamp/(3600*24*365)/4;
//get the day of age
case 'd':
tmpint=time(NULL)-TimeStamp;
return (tmpint/(3600*24)%365)-tmpint/(3600*24*365)/4;
//hours of age
case 'h':
tmpint=time(NULL)-TimeStamp;
return (tmpint%(3600*24))/3600;
//hour of birth
case 'H':
return (TimeStamp%(3600*24))/3600;
//minutes of age
case 'm':
tmpint=time(NULL)-TimeStamp;
return (tmpint%3600)/60;
//minutes of birth
case 'M':
return (TimeStamp%3600)/60;
//total seconds of age
case 'r':
return tmpint=time(NULL)-TimeStamp;
//total seconds of birth (since 1970)
case 'R':
return TimeStamp;
//seconds of age
case 's':
tmpint=time(NULL)-TimeStamp;
return tmpint%60;
//seconds of birth
case 'S':
return TimeStamp%60;
//year of age
case 'y':
tmpint=time(NULL)-TimeStamp;
tmpint=tmpint/(3600*24);
tmpint+=tmpint/365/4;
return tmpint/365;
//year of birth
case 'Y':
tmpint=TimeStamp/(3600*24);
tmpint+=tmpint/365/4;
return (tmpint/365)+1970;
default:
return 0;
}
}
private:
int TimeStamp;
};

int main(){
Human user;
int year=0,month=0,day=0,hour=0,minute=0,second=0;
do{
std::cin.clear();
std::cout<<"Please enter your year of birth\n";
std::cin>>year;
}while(year<1970||year>3000);
do{
std::cin.clear();
std::cout<<"Please enter your month of birth\n";
std::cin>>month;
}while(month<1||month>12);
do{
std::cin.clear();
std::cout<<"Please enter your day of birth\n";
std::cin>>day;
}while(day<1||day>31);
user.setBirthTime(year,month,day,hour,minute,second);
printf("You are %i years, %i months, %i days, %i hours, %i minutes and %i seconds old.\n\n",
user.getAgeInfo('y'),user.DateDataFromDay(user.getAgeInfo('d'),MONTH),user.DateDataFromDay(user.getAgeInfo('d'),DAY),
user.getAgeInfo('h'),user.getAgeInfo('m'),user.getAgeInfo('s'));
std::cin.ignore(std::numeric_limits<int>::max(),'\n');
return 0;
}
This is useless for you maybe, but it is funny
You can set month, year, seconds, minutes, hours, days, everything separately. And then you can get the timestamp it made

2. You're using Dev-Cpp aren't you?

Popular pages Recent additions