im totally stuck

Hello, Im new to c++ (no kidding). Currently my syllabus is at function. so, i was given a task to write a program about calculate the number of days in a year and detecting the leap year without using array(since we are not taught about it yrt). so, here are my code. but the output is super bad....

How to transfer value tday1, tday2 and tday3 from function2 to function3?


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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 #include<iostream>
using namespace std;

void isLeapYear (int y);
void daysInMonth (int d, int m, int y);
void numberOfDays (int d, int m, int t1, int t2, int t3);


int main()
{
	int days, month, year, tday;
	
	cout<<"Welcome to the Days Calculation"<<endl;
	cout<<"We will kindly calculate the number of the days based on your input (dates)"<<endl<<endl;
	
	do
	{
	cout<<"Please input the dates (Day Month Year):"<<endl;
	cin>>days>>month>>year;
	isLeapYear (year);
	daysInMonth (days, month, year);
	
	if ( month < 1 || month > 12 )
	{
		cout << "INVALID: Please input month between 1-12 (Enter the dates again):"<<endl;
	}
	
	if ( days < 1 || days > 31 )
	{
		 cout << "INVALID: Please input day between 1-31 (Enter the dates again):" << endl;
	}
	}
	while (month < 1 || month > 12 || days < 1 || days > 31 );
	
	return 0;
}


void isLeapYear (int y)
{
	if (y%4==0 && y%100!=0)
	{
		cout<<y<<" is a leap year"<<endl;
	}
	
	else if (y%100==0 && y%400==0)
	{
		cout<<y<<" is a leap year"<<endl;
	}
	
	else 
	cout<<y<<" is not a leap year"<<endl;
}


void daysInMonth (int d, int m, int y)
{
	int tday1, tday2, tday3, tday4;
	
	if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10)
	tday1 =31; 
	
 	if (m==2)
	tday2 =28;
	
	if (m==4 || m==6 || m==9 || m==11)
	tday3 =30; 
	

	if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)	
	{
		if (m==2)
		tday2 =29;
	}
	numberOfDays (d, m, tday1, tday2, tday3);
}


void numberOfDays (int d, int m, int t1, int t2, int t3)
{
	int total=0;
	
	if (m==1)
	total = d;
	
	if (m==2)
	total = d + t1;
	
	if(m==3)
	total = d + t1 +t2;
	
	if (m==4)
	total = d + t1 + t1 + t2; 
	
	if (m==5)
	total = d + t1 + t1 + t2 + t3;
	
	if (m==6)
	total = d + t1 + t1 + t2 + t3 + t1;
	
	if (m==7)
	total = d + t1 + t1 + t2 + t3 + t1 + t3;
	
	if (m==8)
	total = d + t1 + t1 + t2 + t3 + t1 + t3 + t1;
	
	if (m==9)
	total = d + t1 + t1 + t2 + t3 + t1 + t3 + t1 + t1;
	
	if (m==10)
	total = d + t1 + t1 + t2 + t3 + t1 + t3 + t1 + t1 + t3;
	
	if (m==11)
	total = d + t1 + t1 + t2 + t3 + t1 + t3 + t1 + t1 + t3 + t1;
	
	if (m==12)
	total = d + t1 + t1 + t2 + t3 + t1 + t3 + t1 + t1 + t3 + t1 + t3;
	
	
	cout<<"the total days "<<total<<endl;
}
Last edited on
write a program about calculate the number of days in a year and detecting the leap year


Work out from year if it's leap:
http://stackoverflow.com/questions/725098/leap-year-calculation

then use that fact to work out how many days are in that year (366 days for leap, otherwise 365.242 days for non-leap)

unless I've missed something in your explanation.
Last edited on
okay, im sorry, my question is not clear. my real problem is at calculating the number of days. the question ask me to use 3 function, which the 1st function is to identify leap year, 2nd function is to identify number of days for every month and the 3rd function for calculating number of days.

im having a problem transfering the number of days in 2nd function to the 3rd function.
ah right. you didn't say that :)

Rather than lines 83 to 117 ..

I'm still not sure what you're adding up in that last function.

cant you just do something like this:

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
int numDays = 0;

if(m==1)
{
    numDays = 31;
} 
else if (m==2)
{
    if(<year is a leap year>)
    {
        numDays = 28;
     } else 
    {
        numDays = 29;
    }


}
else if(m==3)
{
    numDays = 31;
}

...
...

return numDays;


Note, that in here you'll need to know if the year is leap or not. where I've written
<year is a leap year>)

is where you should call your function to take a year and return a bool to say it's leap or not.
Last edited on
okay tq! i will try it :)

the last function is the calculation where i got stuck
Last edited on
Topic archived. No new replies allowed.