Days Instead of Years

Hi,

I am writing a program that tells the user their age on days, however, at the moment, it outputs it in years, how can i make this output in days (im thinking using mktime, and such, but I can not figure out how)?

(P.S. i am very very new at this, so please, don't make fun of my code, I know that its not very efficient and such)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdlib>
#include <iostream>
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <unistd.h>
#include <conio.h>
#include <time.h>
#include <ctime>



using namespace std;
 
void getCurrentDate(int&, int&, int&);
void getBirthday(int&, int&, int&);
int  calcYears(int, int, int, int, int, int);
 
int  main()
{
   int month, day, year, month1, day1, year1, age;
   getCurrentDate(month1, day1, year1);
   getBirthday(month, day, year);
   age = calcYears(year, year1, month, month1, day, day1);
   
   int test1;
   
   
   
    
   cout << age;
   cout << "\n\n\n";
   //cout << "\t" << month << "\t" << day << "\t" << year << endl;
   //cout << "\t" << month1 << "\t" << day1 << "\t" <<year1 << endl;
   system("pause");
   
   
   system("pause");
   
   
   return 0;
}

void getBirthday(int& month, int& day, int& year)
{
   cout << "\nEnter the month you were born on: ";
   cin >> month;
   cout << "\nEnter the day you were born: ";
   cin >> day;
   cout << "\nEnter the year you were born: ";
   cin >> year;
   cout << endl;
    
   return;
}  

void getCurrentDate(int& month1, int& day1, int& year1)
{
     
     
     	time_t t = time(NULL);
	tm* timePtr = localtime(&t);

//  cout << "seconds= " << timePtr->tm_sec << endl;
//  cout << "minutes = " << timePtr->tm_min << endl;
//  cout << "hours = " << timePtr->tm_hour << endl;
//  cout << "day of month = " << timePtr->tm_mday << endl;
//  cout << "month of year = " << timePtr->tm_mon << endl;
//  cout << "year = " << timePtr->tm_year + 1900 << endl;
//  cout << "weekday = " << timePtr->tm_wday << endl;
//  cout << "day of year = " << timePtr->tm_yday << endl;
//  cout << "daylight savings = " << timePtr->tm_isdst << endl;

/*  int*/ year1= timePtr->tm_year + 1900;
/*  int*/ month1= timePtr->tm_mon + 1;
/*  int*/ day1= timePtr->tm_mday;
  
//  cout << "\nEnter the current month: ";
//  cin >> month1;
//  cout << "\nEnter the current day: ";
//  cin >> day1;
//  cout << "\nEnter the current year: ";
//  cin >> year1;
//  cout << endl;
    
   return;
}  
int calcYears(int year, int year1, int month, int month1, int day, int day1)
{
   int years;
    
   years = year1 - year;
   if ((month > month1) || ( month == month1 && day > day1))
       years = years - 1;
    
   return years;
   printf("\n");
}




Thanks,
Stephen
Yes, I need to find the exact number of days.

Can anyone give me an example of this (preferable in context) :)

Thanks,
Stephen
I think this might help you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define isleap(y)		((y) % 4 == 0 && ((y) % 100 || (y) % 400 == 0))

typedef struct {
	int mDay, mMon, mYear;
}Date;

Date *setdate(Date *p, int day, int mon, int year)
{
	return set(p, day, mon, year);
}

Date *setToday(Date *p)
{
	time_t timer = time(NULL);
	struct tm *tp = localtime(&timer);
	return set(p, tp->tm_mday, tp->tm_mon + 1, tp->tm_year + 1900);
}

static const int daytabs[2][13] = {	
                {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};



int getYearDay(const Date *p)
{
      int sum = p->mDay;

      for (int k = 1; k < p->mMon; ++k)
	sum += daytabs[isleap(p->mYear)][k];
	return sum;
}

static int totalDays(const Date *p)
{
	int sum = 0;
	for (int k = YEARBASE; k < p->mYear; k++)
		sum += 365 + isleap(k);

	return sum + getYearDay(p);
}
Last edited on
How exactly is this used?

Thanks,
Stephen
1.isleap:
to check whether a year is a leap year or not.

2.daytabs Array:
I entered information for each month of the year

3.setdate function:
will use to enter a date.

4.setToday function:
Sets the current date.

5.getYearDay function:
Which day of the year

6.totalDays function:
the total number of days
u can use three for loop to calculate the days between two date one birth date secont current date.innermost loop 4 days 2nd loop for months third for year.

http://www.cprogramming.tk
Topic archived. No new replies allowed.