My code is to tell the user whether they entered a leap year or not. I'm not sure how to determine if the user entered letters or something other than a year. Also, no matter what year is entered, it says that it is a leap year.
Looks like there's some mistakes here, such as the two return 0 lines on 41 and 47. The spacing could use some work too; this is why I like Code:Blocks, with it you can just highlight the text in question and hit use AStyle Format.
Just from looking I can see there is an issue with your yearTest function. Shouldn't the line on 50 read "int yearTest (int year)", and the same with the prototype on line 6? Then, you can remove line 52, as the value of year will be specified by the function calls on your earlier lines (such as line 18).
Here's the code with some minor formatting changes and the differences I was referring to.
I also removed the char n=0 on line 53 as it was unnecessary.
#include <iostream>
usingnamespace std;
int yearTest(int year);
int main(int argc, char** argv)
{
int year;
bool Repeat = true;
char YoN;
while (Repeat)
{
cout << "Please enter a year: ";
cin >> year;
if (yearTest(year) == 0)
cout << year << " is not a leap year. " << endl;
elseif (yearTest(year) == 1)
cout << year << " is a leap year. " << endl;
elseif (yearTest(year) == -1)
cout << year << " is not a valid year. " << endl;
cout << "Want to enter another year? (Y/N): ";
cin >> YoN;
if (YoN == 'Y' || YoN == 'y')
Repeat = true;
elseif (YoN == 'N' || YoN == 'n')
return 0;
cout << endl << endl;
}
return 0;
}
int yearTest(int year)
{
if (year < 0)
return -1;
elseif (year % 4 == 0)
{
if ((year % 100 == 0)&&(year % 400 != 0))
return 0;
elsereturn 1;
}
elsereturn 0;
}
Please enter a year: 2000
2000 is a leap year.
Want to enter another year? (Y/N): y
Please enter a year: -1
-1 is not a valid year.
Want to enter another year? (Y/N): y
Please enter a year: 2013
2013 is not a leap year.
Want to enter another year? (Y/N): n
Now that the function is being passed the value of year entered by the user each time, the program works as expected. Also, line 34 could just as easily be "Repeat=false" and the program would still work.
Then chances are your implementation is incorrect.
Use cout statements to see what the values are being calculated.
That one is already solved; take a look at the initial code and you will see the function was improperly set up, by defining the variable type but not the variable itself. Additionally, year was being given a value of 0 each time the function was run, ensuring it would say that it was a leap year.
Still, you make a good point that an if statement such as the one above could be used to consider anything except numbers between 0 and 3000 (or whatever) as invalid entries.