Help With Leap year program

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
/*****************************************************
 M
 6.3 LeapYear.cpp
 T
 *****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
 calculates and displays a message if the year is a leap year.
 The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */

//header files
/* use the correct preprocessor directives for input/output */

#include <iostream>
#include <string>

using namespace std;
bool isLeap();

// user-defined function prototypes
/* the function getYear returns an integer value
 entered by the user */
int getYear(string message);

/* the function isLeap returns a Boolean value
 after processing the value passed to the
 function */
int isLeap(string message);

/* the function moreData returns a character
 value entered by the user */
char moreData(string message);
int main()
{
    // declare variables
    /* declare a integer variable year to enter a year,
     a Boolean variable leapYear to indicate whether
     or not a year is a leap year, and a character
     variable again to indicate if the program
     should continue */
    int year;
    int leapYear;
    char again;
    
    // instruct the user the purpose of the program
    cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
    
    // loop until the user chooses to quit
    while (again == 'y')
    {
        
        // assign to year the value returned from getYear
        year = getYear(" Enter a 4 digit year you would like to check : ");
        /* assign to leapYear the value returned from isLeap
         with year as the argument */
        leapYear = isLeap(" It is a leap.");
        /* if leapYear is true
         if (leapYear)
         display that the year is a leap year
         
         cout << " This is a leap year. " ;
         else
         display that the year is not a leap year */
        cout << " This is not a leap year.";
        
        /* assign to again the character returned from
         moreData converted to lowercase */
        again = tolower(again);
        
    }
    return 0;
    
    /* the function getYear returns an integer value
     entered by the user */
    int getYear(string message);
    {
        // declare an integer variable year to enter a year
        // prompt the user to enter a year
        cout << " Enter a year : ";
        // read in the value entered
        cin >> year;
        // return the value entered
        return year;
    }
    
    /* the function isLeap returns a Boolean value
     after processing the value passed to the
     function */
    bool isLeap(string message);
    
    // if year is divisible by 400 it is a leap year
    /* if year is divisible by 4 but not by 100
     it is a leap year */
    // otherwise the year is not a leap year
    
    {
        if ((year % 4 == 0) && (!(year % 100 == 0)))
            cout << "It is a leap year!" << endl;
        else
            if (year % 400 == 0)
                cout <<" It is a leap year";
        return year;
    }
    
    /* the function moreData returns a character
     value entered by the user */
    char moreData(string message);
    {
        // declare variables
        /* declare a character variable letter to
         enter the user's response */
        char letter;
        // prompt the user if more data is to be entered
        cout << "message";
        // read in user's response
        cin >> letter;
        // return user's response
        return letter;
    }
}

The error i received is "Functions that differ only in their return type cannot be overloaded"
I have no clue in how to go about fixing that error. Please help
The first thing I see is that you seem to be trying to implement your functions inside main(), this is not allowed.

Next your isLeap() function prototype doesn't match your function implementation. You also have two different isLeap() prototypes, but only one isLeap() function implementation.

The first thing I see is that you seem to be trying to implement your functions inside main(), this is not allowed.

Next your isLeap() function prototype doesn't match your function implementation. You also have two different isLeap() prototypes, but only one isLeap() function implementation.

I got rid of the functions in the main. But im confused on the last part. What exactly do i have to change?
You need to change your prototypes, or the function implementation so they match, which one is your decision.
Oh ok i see what you mean. I changed it to match but now it says "Code will never be executed" For these lines of code
1
2
3
4
5
6
7
8
{
        if ((year % 4 == 0) && !(year % 100 == 0))
            cout << "It is a leap year";
        else
            if (year % 400 == 0)
                cout <<" It is a leap year";
        return year;
    }


1
2
3
4
5
6
7
8
9
10
11
12
{
        // declare variables
        /* declare a character variable letter to
         enter the user's response */
        char letter;
        // prompt the user if more data is to be entered
        cout << "message";
        // read in user's response
        cin >> letter;
        // return user's response
        return letter;
    }


1
2
3
4
5
6
7
8
{
        // declare an integer variable year to enter a year
        // prompt the user to enter a year
        cout << " Enter a year : ";
        // read in the value entered
        cin >> year;
        // return the value entered
        return year;

I assumed maybe it was the ";" after each function definition but it doesnt help
Last edited on
You need to post the whole program, the snippets don't provide enough information.

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
// Enter your name as a comment for program identification
// Program assignment LeapYear.cpp
// Enter your class section, and time
/*****************************************************
 M
 6.3 LeapYear.cpp
 T
 *****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
 calculates and displays a message if the year is a leap year.
 The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */

//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
using namespace std;

// user-defined function prototypes
/* the function getYear returns an integer value
 entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
 after processing the value passed to the
 function */
bool isLeap(string message);
/* the function moreData returns a character
 value entered by the user */
char moreData(string message);
int main()
{
    // declare variables
    /* declare a integer variable year to enter a year,
     a Boolean variable leapYear to indicate whether
     or not a year is a leap year, and a character
     variable again to indicate if the program
     should continue */
    int year, leapYear;
    char again;
    // instruct the user the purpose of the program
    cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
    
    // loop until the user chooses to quit
    while (again == 'y')
    {
        
        // assign to year the value returned from getYear
        year = getYear(" Enter a year : ");
        /* assign to leapYear the value returned from isLeap
         with year as the argument */
        leapYear = isLeap(" It is a leap.");
        /* if leapYear is true
         if (leapYear)
         display that the year is a leap year
         cout << “ This is a leap year. “;
         else
         display that the year is not a leap year */
        cout << " This is not a leap year.";
        
        /* assign to again the character returned from
         moreData converted to lowercase */
        again = tolower(again);
        
    }
    return 0;
    
    /* the function getYear returns an integer value
     entered by the user */
    int getYear(string message);
    {
        // declare an integer variable year to enter a year
        // prompt the user to enter a year
        cout << " Enter a year : ";
        // read in the value entered
        cin >> year;
        // return the value entered
        return year;
    }
    /* the function isLeap returns a Boolean value
     after processing the value passed to the
     function */
    bool isLeap(string message);
    // if year is divisible by 400 it is a leap year
    /* if year is divisible by 4 but not by 100
     it is a leap year */
    // otherwise the year is not a leap year
    {
        if ((year % 4 == 0) && !(year % 100 == 0))
            cout << "It is a leap year";
        else
            if (year % 400 == 0)
                cout <<" It is a leap year";
        return year;
    }
    
    /* the function moreData returns a character
     value entered by the user */
    char moreData(string message);
    {
        // declare variables
        /* declare a character variable letter to
         enter the user's response */
        char letter;
        // prompt the user if more data is to be entered
        cout << "message";
        // read in user's response
        cin >> letter;
        // return user's response
        return letter;
    }
}
You still are trying to implement your functions in another function. Where is your terminating brace } for main()?

line 113.
Sorry for being difficult to guide. Im still trying to understand the functions concept
That's the problem, it shouldn't be there it should be much earlier, perhaps somewhere before your function implementations? Your functions must be implemented outside any other function, including main()!
I changed the main function to end at line 61. And now i get a "expected unqualified-id" error at the brace f=before each function.
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
/* The program LeapYear.cpp prompts the user to input a year, then
 calculates and displays a message if the year is a leap year.
 The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */

//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>
using namespace std;

// user-defined function prototypes
/* the function getYear returns an integer value
 entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
 after processing the value passed to the
 function */
bool isLeap(string message);
/* the function moreData returns a character
 value entered by the user */
char moreData(string message);
int main()
{
    // declare variables
    /* declare a integer variable year to enter a year,
     a Boolean variable leapYear to indicate whether
     or not a year is a leap year, and a character
     variable again to indicate if the program
     should continue */
    int year, leapYear;
    char again;
    // instruct the user the purpose of the program
    cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
    
    // loop until the user chooses to quit
    while (again == 'y')
    {
        
        // assign to year the value returned from getYear
        year = getYear(" Enter a year : ");
        /* assign to leapYear the value returned from isLeap
         with year as the argument */
        leapYear = isLeap(" It is a leap.");
        /* if leapYear is true
         if (leapYear)
         display that the year is a leap year
         cout << “ This is a leap year. “;
         else
         display that the year is not a leap year */
        cout << " This is not a leap year.";
        
        /* assign to again the character returned from
         moreData converted to lowercase */
        again = tolower(again);
        
    }

    return 0;
}
    /* the function getYear returns an integer value
     entered by the user */
    int getYear(string message);
    {
        // declare an integer variable year to enter a year
        // prompt the user to enter a year
        cout << " Enter a year : ";
        // read in the value entered
        cin >> year;
        // return the value entered
        return year;
    }
    /* the function isLeap returns a Boolean value
     after processing the value passed to the
     function */
    bool isLeap(string message);
    
    // if year is divisible by 400 it is a leap year
    /* if year is divisible by 4 but not by 100
     it is a leap year */
    // otherwise the year is not a leap year

    {
        if ((year % 4 == 0) && !(year % 100 == 0))
            cout << "It is a leap year";
        else
            if (year % 400 == 0)
                cout <<" It is a leap year";
        return year;
    }
    
    /* the function moreData returns a character
     value entered by the user */
    char moreData(string message);
            
    {
        // declare variables
        /* declare a character variable letter to
         enter the user's response */
        char letter;
        // prompt the user if more data is to be entered
        cout << "message";
        // read in user's response
        cin >> letter;
        // return user's response
        return letter;
    }
That's because you have a semicolon following all of the implementations.
Which exact semicolons should i remove
I'd start with the ones that are right before the position being indicated by your error messages.
Immediately after doing so. I got 5 errors saying undeclared identifier "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
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
// Enter your name as a comment for program identification
// Program assignment LeapYear.cpp
// Enter your class section, and time
/*****************************************************
 M
 6.3 LeapYear.cpp
 T
 *****************************************************/
/* The program LeapYear.cpp prompts the user to input a year, then
 calculates and displays a message if the year is a leap year.
 The program utilizes user-defined functions.*/
/* A year and a character for a loop to determine if the program continues. */
/* A message displaying if the date is a leap year. */

//header files
/* use the correct preprocessor directives for input/output */
#include <iostream>
#include <string>

using namespace std;

// user-defined function prototypes
/* the function getYear returns an integer value
 entered by the user */
int getYear(string message);
/* the function isLeap returns a Boolean value
 after processing the value passed to the
 function */
bool isLeap(string message);
/* the function moreData returns a character
 value entered by the user */
char moreData(string message);
int main()
{
    // declare variables
    /* declare a integer variable year to enter a year,
     a Boolean variable leapYear to indicate whether
     or not a year is a leap year, and a character
     variable again to indicate if the program
     should continue */
    int year, leapYear;
    char again;
    // instruct the user the purpose of the program
    cout << " This program will ask the user to enter a year and then determines whether it is a leap year. \n";
    
    // loop until the user chooses to quit
    while (again == 'y')
    {
        
        // assign to year the value returned from getYear
        year = getYear(" Enter a year : ");
        /* assign to leapYear the value returned from isLeap
         with year as the argument */
        leapYear = isLeap(" It is a leap.");
        /* if leapYear is true
         if (leapYear)
         display that the year is a leap year
         cout << “ This is a leap year. “;
         else
         display that the year is not a leap year */
        cout << " This is not a leap year.";
        
        /* assign to again the character returned from
         moreData converted to lowercase */
        again = tolower(again);
        
    }

    return 0;
}
    /* the function getYear returns an integer value
     entered by the user */
    int getYear(string message)
    {
        // declare an integer variable year to enter a year
        // prompt the user to enter a year
        cout << " Enter a year : ";
        // read in the value entered
        cin >> year;
        // return the value entered
        return year;
    }
    /* the function isLeap returns a Boolean value
     after processing the value passed to the
     function */
    bool isLeap(string message)
    
    // if year is divisible by 400 it is a leap year
    /* if year is divisible by 4 but not by 100
     it is a leap year */
    // otherwise the year is not a leap year

    {
        if ((year % 4 == 0) && !(year % 100 == 0))
            cout << "It is a leap year";
        else
            if (year % 400 == 0)
                cout <<" It is a leap year";
        return year;
    }
    
    /* the function moreData returns a character
     value entered by the user */
    char moreData(string message)
            
    {
        // declare variables
        /* declare a character variable letter to
         enter the user's response */
        char letter;
        // prompt the user if more data is to be entered
        cout << "message";
        // read in user's response
        cin >> letter;
        // return user's response
        return letter;
    }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
	int year;
	cin >> year;
	bool b = false;
	if(year%4==0) b=true;
	if( year%100==0 && year%400 !=0 ) b= false;
	
	if(b) cout << "Leap Year\n";
	else cout << "Not a Leap Year\n";
	
return 0;
}


leap year: 2012, 2000, 2400
not: 1900, 2014, 2100, 2200
Last edited on
Topic archived. No new replies allowed.