-2

I'm having compilation problems. I can't see where my error came from. Can, someone help me? The goal of this code is to ingress a date and to check if it's right.

   #include<iostream>
   #include<math.h>
   #include<cmath>
   using namespace std;

int main(){

    int d, m, a;
    cout << "entrar dia"<<endl;
    cin>>d;
    cout << "entrar mes"<<endl;
    cin>>m;
    cout << "entrar ano"<<endl;
    cin>>a;
    if(d<1 || d>32|| m<1 || m>13 || a<1960 || a>2022) {
        cout<<"Fecha incorrecta"<<endl;
        else {

            if( (m==4 || m==6 || m==9 || m==11) && d>30){
                cout<<"Fecha incorrecta"<<endl;
            }
        }
        else {


            if((m == 2 && a % 4 = 0 && d > 28) || (m == 2 && a % 4 == 0 && d > 29)) {
                cout<< "Fecha incorrecta"<<endl;
            }
        }

        else {
                cout <<"Fecha correcta"<<endl;
        }

    }}

Thanks

1 Answers1

2

You should really use nested if statements. This helps keep your conditional logic clean, unfortunately, you have else clause's out of their expected sequence. Also your final else if had an assignment operator = instead of the equality operator ==

#include<iostream>
#include<math.h>
#include<cmath>

using namespace std;

int main ()
{
  int d, m, a;
  cout << "entrar dia" << endl;
  cin >> d;
  cout << "entrar mes" << endl;
  cin >> m;
  cout << "entrar ano" << endl;
  cin >> a;
  if (d < 1 || d > 32 || m < 1 || m > 13 || a < 1960 || a > 2022)
  {
    cout << "Fecha incorrecta" << endl;
  }
  else if ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
  {
    cout << "Fecha incorrecta" << endl;
  }
  else if ((m == 2 && a % 4 == 0 && d > 28)
      || (m == 2 && a % 4 == 0 && d > 29))
  {
      cout << "Fecha incorrecta" << endl;
  }
  else
  {
      cout << "Fecha correcta" << endl;
  }
}
Eat at Joes
  • 4,937
  • 1
  • 40
  • 40