Checking for date in c++ -


#include <iostream> #include <stdio.h> #include <cstdlib>  using namespace std;  int main() {      int d,m,g;     int correctly=1;    cout<<"insert date \n";    cin>>d;    cin>>m;    cin>>g;     if (d<1 || m<1 || m>12) correctly=0;    int numberofdays;    switch(m)  {        case 1:          case 3:            case 5:              case 7:                case 8:                  case 10:                    case 12:                     numberofdays=31;          break;              case 4:              case 6:                case 9:                  case 11:               numberofdays=30;               break;            case 2:             if (g%4==0 && g%100=0 || g%400==0)                 numberofdays=29;             else                 numberofdays=28;              break;              default:             numberofdays=0; }  if (d>numberofdays) correctly=0;  if (correctly==1)     cout<<"date good.\n";      else     cout<<"date not good.\n";   system ("pause"); return exit_success;   } 

i'm getting errors while trying change value of correctly ones or zeros , been getting error in case 2 line while trying change 0 !=0.and dont understand how part correctly works.

how instead of switch case:

if (m>12 || m<1)     numberofdays=0; else{     int days[]={31,28,31,30,31,30,31,31,30,31,30,31};     numberofdays=days[m-1]+(m==2 && (g%4==0 && g%100!=0 || g%400==0))?1:0; } 

if don't know ternary:

if (m>12 || m<1)     numberofdays=0; else{     int days[]={31,28,31,30,31,30,31,31,30,31,30,31};     numberofdays=days[m-1];     if(m==2 && (g%4==0 && g%100!=0 || g%400==0))         numberofdays++; } 

edit:

some more tips. mentioned in comments, meant use d,m,y instead of d,m,g. don't validate year , can move code function, don't need correctly variable.

bool validatedate(int d, int m, int g){ //used g don't confused     if(d<1 || d>31 || m<1 || m>12 || g<0) //not sure if can have year 0         return false; //returns false don't waste time checking other stuff     int days[]={31,28,31,30,31,30,31,31,30,31,30,31};     int numberofdays=days[m-1];     if(m==2 && (g%4==0 && g%100!=0 || g%400==0))         numberofdays++;     return d<=numberofdays;     //should add current format leap years started @ year (google says 1582), might have adjust that. } 

call main function by:

if (validatedate(d,m,g))    cout<<"date good.\n"; else    cout<<"date not good.\n"; 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -