• Support PF! Buy your school textbooks, materials and every day products Here!

C++: Leap Year

  • Comp Sci
  • Thread starter lkh1986
  • Start date
  • #1
99
0
1. Homework Statement
I just started my C++ courses and here is my question.



2. Homework Equations



3. The Attempt at a Solution
//This program states whether a given year is a leap year or a non-leap year.

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

void main ()
{
int year, value;
char answer,y, n;

while (answer == y)
{
cout << "Please input a year: ";
value = year/4;
if (value==int)
cout << ""<< year << " is a leap year. There are 29 days in Feb " << year <<".";
else
cout << ""<< year << " is a non-leap year. There are 28 days in Feb " << year <<".";
}
cout << "Do you want to continue? Press 'y' if yes or 'n' if no.";
getch();
}

It seems that "if (value==int)" is illegal. Is there any other way I can put th expression statement so that the computer can print out whether a given year is a leap year or not?

Thanks.
 

Answers and Replies

  • #2
161
0
What you need to do is to check if the variable year is a multiple of 4 and the modulus operator % is perfect for the task, e.g.,
1%4 = 1, 2%4 = 2, 3%4 = 3, 4%4 = 0, 5%4 = 1, ...

What should the conditional expression be for the if-statement now then?

Btw, the rules for checking a leap year is a bit more involved than the above. Look http://en.wikipedia.org/wiki/Leap_year" [Broken] for an insight.
 
Last edited by a moderator:
  • #3
99
0
Okay, thanks for the help. My instructor just introduced the modulus operator today.
 

Related Threads for: C++: Leap Year

  • Last Post
Replies
4
Views
25K
Replies
3
Views
840
  • Last Post
Replies
1
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
6K
Replies
19
Views
9K
Top