Leap Year Calculations

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.

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

#include <iostream>

using namespace std;

int yearTest(int);

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;
		}
		if (yearTest(year) == 1)
		{
			cout << year << " is a leap year. " << endl;
		}
		if (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;
		else if (YoN == 'N' || YoN == 'n')
			return 0;

		cout << endl << endl;
	}
	return 0;
	
	

	
	
	return 0;
}

int yearTest(int)
{
		int year = 0;
	    char n = 0;
	
		
		if (year == 'n')
		{
			return -1;

		}
		else if (year < 0)
		{
			return -1;
		}
		
		else if (year % 4 == 0)
		{
			
			if ((year % 100 == 0)&&(year % 400 != 0))
			{	
			 return 0;
			}
			else
			{		
			return 1;
			}
			
			
		}
		else
		{
				return 0;
		}
		
		 
		 

	
}
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.
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
#include <iostream>

using namespace 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;
      
        else if (yearTest(year) == 1)
            cout << year << " is a leap year. " << endl;
        
        else if (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;
        else if (YoN == 'N' || YoN == 'n')
            return 0;

        cout << endl << endl;
    }
    return 0;
}

int yearTest(int year)
{
    if (year < 0)
        return -1;
    else if (year % 4 == 0)
    {
        if ((year % 100 == 0)&&(year % 400 != 0))
            return 0;
        else
            return 1;
    }
    else
        return 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.
Last edited on
I'm not sure how to determine if the user entered letters or something other than a year.


You would use a if statement

if ((year > 0) && (year <=3000))

Also, no matter what year is entered, it says that it is a leap year.


Then chances are your implementation is incorrect.
Use cout statements to see what the values are being calculated.

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.
Last edited on
Thank you very much!!
Topic archived. No new replies allowed.