Leap Year program

May 27, 2010 at 12:57am
Ok this is what I have to do.
Write a program to find if a year is a leap year. The user will input the year and the system will have to do the verification. I have to write a separate function from main().

This is what I have so far.

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
#include <iostream> // allows program to perform input and output
using namespace std; // program uses names from the std namespace

int leapyear (int yr)
{
	
			if ((yr % 4 == 0) && !(yr % 100 == 0))
			cout<<yr;	
			else if (yr % 400 == 0)  // year divisible by 4 but not by 100
			cout<<yr;
			return yr;

}

int main()
{
	const int arraySize = 5; // arraySize must be const
	int year[ arraySize ]; // declare array for input

	cout << "Enter " << arraySize << " four digits years:\n";
	for ( int i = 0; i < arraySize; i++ )
    cin >> year[ i ];

	cout << leapyear(year)<<" is a leap year.\n" << endl;
	


  
} // end main 


Now my question is how do I get the function to get each value of the user input.

Any help will be appreciated..

Thanks
Last edited on May 27, 2010 at 12:58am
May 27, 2010 at 2:09am
1. What are the stray "cout<<yr"s doing in leapyour? Those don't belong there.
2. You don't need to ask the user for five numbers. Each cin operation will read an entire number, not just a digit.
3. Since leapyear determines whether or not a year is a leap year, it should return true/false, not the year itself.
May 27, 2010 at 10:24pm
The assignment says to create a program where the user input 5 years and the program will verify which one is leap year and which not. It will have to print on screen the answers.

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
#include <iostream> // allows program to perform input and output
using namespace std; // program uses names from the std namespace

int leapyear (int);

int main()
{
	const int arraySize = 5; // arraySize must be const
	int year[ arraySize ]; // declare array for input
	int i;

	cout << "Enter " << arraySize << " four digits years:\n";
	for ( i = 0; i < arraySize; i++ )
		cin >> year[ i ];

	

	
		
	cout<<leapyear(year[i])<<"is a leap year.\n";
	
} // end main

int leapyear (int yr)
{
	int leap = 0;
	int notLeap = -1;

	
	
		if ((yr % 4 == 0) && !(yr % 100 == 0)|| (yr % 400 == 0))
			leap = yr;
		else
			leap = notLeap;

	
	return leap;

}




I got it to run but I got a huge number as an answer. Can anyone tell me why?

"-858993460 is a leap year"


Please help me. Tell me exactly what is wrong with my code. And if you can explain it, I will appreciated a lot.

Thanks
May 28, 2010 at 4:29am
line 20
cout<<leapyear(year[i])<<"is a leap year.\n";
Are you syre that you want an output like "-1 is a leap year." ?

line 32
leap = yr
Are you trying to assign the value of year to leap ?

I strongly recommend using a bool function like this-
1
2
3
4
5
6
7
8
9
bool isALeapYear( int year )
{
	/* Check if the year is divisible by 4 or 
	is divisible by 400 */
	if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
		return true;
	else
		return false;
}
Last edited on May 28, 2010 at 7:02am
May 29, 2010 at 7:40am
1. We're not here to do your homework for you.
2. The leap year formula is all over Google... why don't you go search for it?
3. You don't need to store the year number in an array... just store it as an integer and pass it to your function.
4. Why in the hell are you passing a single digit to your function? That formula isn't going to work with single digits. Use the entire year number.
Last edited on May 29, 2010 at 7:42am
May 29, 2010 at 11:43am
at packetpirate:
1: this is a forum and I was asking for help. I didnt expect anyone to do my homework.

2. I figured it out by myself.


3. Thanks to you guys who gave me some good tips.


ps. im not thanking you packetpirate! you were not help at all!
May 29, 2010 at 3:15pm
I strongly recommend using a bool function like this-

If I may, whenever you need a function to return a bool value that is the direct result of an expression, you can say it directly, like this:

1
2
3
4
5
6
bool isALeapYear( int year )
{
	/* Check if the year is divisible by 4 or 
	is divisible by 400 */
	return ((year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0));
}
May 30, 2010 at 5:54am
filipe wrote:

If I may, whenever you need a function to return a bool value that is the direct result of an expression, you can say it directly, like this:


Yes but that does not quite help the readability of the program. Anyways, Thanks for that tip.
Last edited on May 30, 2010 at 5:55am
Topic archived. No new replies allowed.