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().
#include <iostream> // allows program to perform input and output
usingnamespace std; // program uses names from the std namespace
int leapyear (int yr)
{
if ((yr % 4 == 0) && !(yr % 100 == 0))
cout<<yr;
elseif (yr % 400 == 0) // year divisible by 4 but not by 100
cout<<yr;
return yr;
}
int main()
{
constint 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
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.
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.
#include <iostream> // allows program to perform input and output
usingnamespace std; // program uses names from the std namespace
int leapyear (int);
int main()
{
constint 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;
}
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))
returntrue;
elsereturnfalse;
}
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.
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));
}