-2

I am new in programming and I need a little help at a problem in C++ .

The problem is :

I need to read 3 numbers and to determinate if this numbers can be a date or not . I need to say "YES" if the numbers can be a date or "NOT" if they can`t be a date.

I've tried this :

#include <iostream>

using namespace std;

int main(){ 

    unsigned int z, l, a;
    cin >> z >> l >> a;

    if((z<32 && l==1) || (z==29 && l==2 && a%4==0) ||
       (z<29 && l==2 && a%4>0) ||(z<32 && l==3) ||
       (z<31 && l==4) || (z<32 && l==5) || (z<31 && l==6) || 
       (z<32 && l==7) || (z<31 && l==8) || (z<32 && l==9) ||
       (z<31 && l==10) || (z<31 && l==1) || (z<31 && l==12)) cout << "YES";
    else cout << "NO";

    return 0;

} 

Question:

Could you help me find the missed cases?

Note:

My teacher commented that "It is almost done but you miss some cases". I tried to find this cases 2 hours but I didn't succeed ...

Ziezi
  • 6,375
  • 3
  • 39
  • 49
L.Cosmin
  • 17
  • 3

1 Answers1

1

Quick answer:

The missed cases probably are due to the missed leap years as your code currently doesn't use correct checking for that. The right check for leap year is at the end of the answer.

Firstly, try and figure out what are the cases on a piece of paper:

date is consisted of positive numbers

start date of Gregorian calendar (add comment to inform the user for interval of valid dates)

Formulate the format, e.g. it could be: dd/mm/yyyy or mm/dd/yyyy, i.e. 1.03.2015

which months have 30, 31, 28, 29 (in which years) days

leap years and February

Regarding the code:

1.Use meaningful variables that have names that explain their purpose, i.e.:

replace unsigned int z, l, a; with variables like: int month, day, year;

2.Create separate if- else if statements for each of the above cases and add comments to indicate them (it will make your code easy to read and understand).

// check if January has 31 days
if(z<32 && l==1){

// check if February has 29 days and it isn't a leap year 
} else if (z==29 && l==2 && a%4==0){

} //...

Bugs in your code:

1.Your current check for leap year: a % 4 == 0 is not entirely correct. A proper check for leap year looks something like:

if year modulo 400 is 0 then
is_leap_year

else if year modulo 100 is 0 then
not_leap_year

else if year modulo 4 is 0 then
is_leap_year

else
not_leap_year

and in code:

if(((year % 4) == 0) && (((year % 100)!=0) || ((year % 400) == 0)){
    //leap year
}

2.The check for November is not right: (z < 31 && l == 1). It should be:

(z < 31 && l == 11)
Community
  • 1
  • 1
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • you've ommited the `if year modulo 1000 is 0 then is_leap_year` case – CommanderBubble Oct 25 '15 at 10:29
  • `(((year % 100) != 0)` is actually covering this case. – Ziezi Oct 25 '15 at 10:37
  • that statement says that every year divisible by 100 isn't a leap year, and that is correct, BUT every year that is divisible by 1000 is a leap year, which is not covered by that. the statement should be `if (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0) || ((year % 1000) == 0)))` – CommanderBubble Nov 03 '15 at 22:22
  • check these: https://support.microsoft.com/en-us/kb/214019 , https://en.wikipedia.org/wiki/Leap_year, no one mentions division by 1000 as a criterion for a leap year. – Ziezi Nov 04 '15 at 07:57
  • Fair enough, I'd always been under the impression that divisible by 1000 was a criteria as well. Wonder where I picked that up... As it stands, I stand corrected then :) – CommanderBubble Nov 05 '15 at 02:23