Leap year whats missing on my program???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{int year;
ofstream output ("e:result14.dat");
cout <<" Enter Year : "<<endl;
output<<" Enter Year : "<<endl;
cin>>year;
if(year%4==0&&year%100!=0 ||year%400==0)
cout<<" this is a leap year\n"<<endl;
output<<" this is a leap year\n"<<endl;
else
cout<<" this is not a leap year\n"<<endl;
output<<" this is not a leap year\n"<<endl;


      system("PAUSE");
      return 0;
}
1
2
3
#include <iostream>
#include <fstream>
using namespace std;


Also, "e:result14.dat" should probably be "e:\result14.dat" or just "result14.dat"
It's obviously missing identation and whitespace, but aside from that, you need parenthesis here:

if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

You should wrap it in a function, though:

1
2
3
4
bool isLeapYear(int year)
{
	return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
i have all that # include....
but to save my output on the e drive its fine...
i am getting on the else statement ...
You're also missing curly braces to wrap the statements after the if and else:

1
2
3
4
5
6
7
8
9
if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
    cout << " this is a leap year\n" << endl;
    output << " this is a leap year\n" << endl;
}
else
{
    // ...
}
yes thanks derrrrr... how could i forget the { braces ...
the program works fine
last question.
when i go and put the year on the cin and go and to print out my output the year that i am entering is not showing .... it just shows:
enter year:
this is a leap year.
what can i do for the year to show on the out put??
1
2
3
std::cout << "Enter year:" << std::endl;
std::cin >> year;
output << "Enter year:" << std::endl << year << std::endl;

Or something like that.
( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

isn't the year % 4 == 0 going to check to see if it's %400, just awhile down the road. So wouldn't the %400 be kinda redundant here?
no, because the %100 check would counter it
A leap year is a multiple of 4 that is not also a multiple of 100, except for multiples of 400. So, for instance, 1900 was not a leap year, but 2000 was.
oh i see, that makes sense now, i kind of quickly glanced at it, but since you guys mentioned it, I see exactly what you're saying

EDIT:
Thanks for the clarification though
Last edited on
Topic archived. No new replies allowed.