Functions?

Write your question here.


Okay so I wrote the first code for leap years a few weeks ago, but I would like to know how to properly define and use the function that I have for the problem, that I included a few lines under the original code.

This is the first time I have ever used functions, and my professor said when we return from break that we'll be using functions, so I was looking for help on how to do functions the right way?




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
//CSC150 Lab1
//Ian Heinze
//10/2/2015

#include <iostream>
using namespace std;

int main( )
{
    int year;

    while (year != 0)
       {
       cout << "\nEnter the year (0 to stop) ";
       cin >> year;

       if ((year % 400 != 0) && (year % 25 == 0))           // Determins that it is not a leap year if divisable by 25, but not by 400
      {
          cout << year << " is not a leap year.\n";
      }
      else if (year % 4 != 0)
      {
          cout << year << " is not a leap year.\n";
      }

       else if ((year % 4 == 0 && year != 0))
      {
          cout << year << " is a leap year.\n";
      }


      else
      {cout << "Bye.\n";
      break;                          // Breaks up the loop so the user can decide if they want to enter in another year
      }

}

       return 0;
    }
    
    
    
    
bool leapYear (int)
{
    if ((( Year % 4 == 0) && (! ( Year % 100 == 0))) || (( Year % 4 == 0) && (! ( Year % 100 == 0))&&( Year % 400 == 0)))
     return true;

    else
        return false;
}
Last edited on
Line 10: year is an uninitialized variable.

Line 12: The first time through the loop, you're testing an uninitialized variable.

Line 17: What's with the modulo 25?

Line 26: year 0 is a leap year

Line 45: You need a function prototype since the function definition of leapYear() occurs after main.
Line 45: leapYear() takes an argument. You must give the argument a name (int Year).

You never call your leapYear() function.
Lines 17:29 can be replaced by
1
2
3
4
  if (leapYear(year))
    cout << year << " is a leap year.\n";
  else
    cout << year << " is not a leap year.\n";

Last edited on
Okay so this is what I have now, and the only error I'm getting is on line 25. Where do I need to move the else if statement to make this program error free? Or what do I need to do to change it?


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
//CSC150 Lab1
//Ian Heinze
//10/2/2015

#include <iostream>
using namespace std;
bool is_leap_year (int year);

int main( )
{
    int year;

    while (year != 0)
       {
       cout << "\nEnter the year (0 to stop) ";
       cin >> year;

       if (is_leap_year(year))
        cout << year << "is a leap year";

       else
        cout << year << "is not a leap year";


      else if (year = 0)
      {cout << "Bye.\n";
      break;                          // Breaks up the loop so the user can decide if they want to enter in another year
      }

}

       return 0;
    }




bool is_leap_year (int year)
{
    if ((year % 400 != 0) && (year % 25 == 0))           // Determins that it is not a leap year if divisable by 25, but not by 400
      {
          return 1;
      }
      else if (year % 4 != 0)
      {
          return 1;
      }

       else if ((year % 4 == 0 && year != 0))
      {
          return 0;
      }



 }
Last edited on
Line 25: Get rid of the else. It's not associated with an if.

Line 11: year is still an uninitialized variable (contains garbage). The first time through the condition at line 13, you are going to get unpredictable results. What happens if year happens to contain 0 when you run your program?

Lines 25-28: This test should be immediately after line 16. If the user enters 0, you don't want to display whether year 0 is a leap year or not (which you do currently).

Lines 40-52: Check your algorithm.
wikipedia wrote:

if (year is not exactly divisible by 4) then (it is a common year)
else
if (year is not exactly divisible by 100) then (it is a leap year)
else
if (year is not exactly divisible by 400) then (it is a common year)
else (it is a leap year)

Topic archived. No new replies allowed.