By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,335 Members | 2,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,335 IT Pros & Developers. It's quick & easy.

Write a program that reads a string from the keyboard and tests whether it contains a

P: 2
i got problem of this question :

The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. February has 28 days except for leap years when it has 29. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
Aug 30 '16 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
What don't you understand about the question?
Aug 31 '16 #2

P: 2
#include <iostream>
using namespace std;

int main()
{
int day, month, year;


cout << "Enter the day :";
cin >> day;
cout << "Enter the month :";
cin >> month;
cout << "Enter the year :";
cin >> year;

if (year % 4 == 0 || year % 100 != 0 || year % 400 == 0)
{
cout << "It`s valid date" <<endl;
}
else(year % 4 !=0 || year % 100 =0 ||year % 400 ==0);
{

cout << "It`s not valid date" <<endl;
}
return 0;
}
i got code like this but when i run it. it doesn`t work
Aug 31 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
Expand|Select|Wrap|Line Numbers
  1. if (year % 4 == 0 || year % 100 != 0 || year % 400 == 0)
  2.  {etc...
says that if the year is divisible by 4 OR the year is not divisible by 100 OR the year is divisible by 400, then the year is a leap year.

The way the compiler codes this is to stop evaluating the IF when the expression becomes true.

In this case any year divisible by 4 will be a valid leap year. So 2012 is a leap year.

Then look at 2011. Not divisible by 4 so the evaluation of the IF continues to OR the year not divisible by 100. 2011 is not divisible by 100 so it is also a leap year.

Not good.

You have to check the year this way:

1) is the year divisible by 4? If yes go to step 2. Otherwise, the year is not a leap year.

2) is the year from step 1 divisible by 100. If not, the year is a leap year. If divisible by 100, go to step 3.

3) Is the year from step 2 divisible by 400? If it is, the year is a leap year. Otherwise it is not.

Post again if you need more help.
Aug 31 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.