Leap Year Program

Jan 26, 2014 at 2:20pm
I have run this program many times where it ask for my input then exits right after i input something, Im not sure if the function is being called or not, I also tried the getch command to get the window to stay open and nothing. Any help would be appreciated.



#include <iostream>
#include <cmath>
#include <conio.h>

using namespace std;

bool leap_year(int year)
{
bool leap;
if ((year % 4 == 0 && year % 100!=0) || (year % 400 == 0) || (year > 1582))
{

return true;
}
else
{

return false;

}
}
int main()
{
cout<<"Enter a Year: ";
int num;
cin>> num;


bool leap_year(int year);
{
return true;
cout << num << "is a leap year.";

return false;
cout << num << "is not a leap year.";

return 0;
}
}
Jan 26, 2014 at 2:33pm
This is my solution;

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
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <conio.h>

using namespace std;

bool leap_year(int);

int main()
{
	int year;
	cout<<"Enter a Year: ";
	cin>> year;

	if (leap_year(year) == true)
		cout << year << " is a leap year.";
	else
		cout << year << " is not a leap year.";
	
	_getch();
	return 0;
}

bool leap_year(int year)
{
	if (((year % 4 == 0 && year % 100!=0) || (year % 400 == 0)) && (year > 1582))
		return true;
	else 
		return false;
}


Have a look at it. Then if you have anything to ask, feel free.
Jan 26, 2014 at 2:40pm
Here's your code, with [code][/code] tags and some indentation:
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
#include <iostream>
#include <cmath>
#include <conio.h>

using namespace std;

bool leap_year(int year)
{
    bool leap;
    if ((year % 4 == 0 && year % 100!=0) || (year % 400 == 0) || (year > 1582))
    {
        return true;
    }
    else
    {
        return false;
    }
}
int main()
{
    cout<<"Enter a Year: ";
    int num;
    cin>> num;

    bool leap_year(int year);

    {
        return true;
        cout << num << "is a leap year.";

        return false;
        cout << num << "is not a leap year.";

        return 0;
    }
}

Let's step through your program, starting at the beginning of main():
1) Prints "Enter a year: "
2) Declares an int variable named num
3) Asks for input for that variable
4) The statement bool leap_year(int year); doesn't do anything. You can't define functions inside of main(), and even if you could, it would still be an error because of the semicolon.
5) The curly brace after that line is the start of a new block, but for all intents and purposes, that doesn't have any effect on your program.
6) The program returns true (1, probably) at this point.

Oh, and by the way, your is_leap function isn't even correct:
if ((year % 4 == 0 && year % 100!=0) || (year % 400 == 0) || (year > 1582))
You're saying that any year past 1582 is a leap year.
Last edited on Jan 26, 2014 at 2:47pm
Jan 26, 2014 at 2:47pm
@Pierreseoul:
And also to add to the list there's a logic error in your leap year calculation, which i changed in my own code. Have another look at your calculation, then mine and try to find it yourself. If you can't feel free to ask again.
Last edited on Jan 26, 2014 at 2:48pm
Jan 26, 2014 at 3:28pm
Thank you quine i noticed the area changing || to &&.. Just to make sure i understand it the main problem i I had was trying to declare the function outside of main? Its so weird because the example in my book shows it that way. Thank you very much.
Jan 26, 2014 at 3:39pm
I think what happened was that you tried to define your function inside of main(), like this:
1
2
3
4
5
6
7
8
int main()
{
    // Stuff here
    bool leap_year(int year)
    {
        // Stuff here
    }
}

This is an error, since you can't define functions inside of other functions (unless it's a lambda, but that's a different story...).
Then, you probably somehow figured out that you could get it to compile by adding a semicolon after the bool leap_year(int year) part.
That works, but not how you would expect it to....

What's the example in your book?
(Are you sure you read it correctly?)
Jan 26, 2014 at 3:46pm
The example in my book is as follows:
#include
#include

using name space
//Function here

int main()
{

output asking for a value

//Call function here

return 0;
}
Jan 26, 2014 at 3:51pm
All the calculations for the function were declared outside of main
Jan 26, 2014 at 3:52pm
The example in your book is right. But your code is wrong.
Because in the book it says, define your function first (outside of main) then call it inside your main.

Better explanation of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include
#include
using name space

type function (variable);  //this is called a prototype.

int main()
{
output asking for a value

function(value);//Call function here

return 0;
}

type function (variable)
{
//code block.
}
Jan 26, 2014 at 3:56pm
@Pierreseoul
So something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void func(int i)
{
    std::cout << i;
}
int main()
{
    std::cout << "Enter a number: ";
    int num;
    std::cin >> num;
    
    func(num); // This? Or does it have
    //void func(int i) { ... } here?
}
?

You call a function by writing its name followed by whatever you want to pass to the function (the arguments) in parenthesis.
That's different from a declaration/definition, which has a return type along with the list of parameters.
Last edited on Jan 26, 2014 at 3:56pm
Topic archived. No new replies allowed.