Hi,
When i write any code has if else statement , and run it na error appeatrs and say that else doesn't match with if or illegal else without matching if.
I don't know where is the problem exctly ,and you should know that i don't put } in these program.
For example this program-

#include<iostream>
using namespace std;
int main(){  
	int year ,month,day,leapyear;
	cout<<"Enter a year and month ";
	cin>>month>>year;
	if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))){
		leapyear=1;
	   cout<<"This is aleap year";
	   else{
	   cout<<"This is not leap year";
	   }
	}
return 0;
}

Recommended Answers

All 3 Replies

Hi,
When i write any code has if else statement , and run it na error appeatrs and say that else doesn't match with if or illegal else without matching if.
I don't know where is the problem exctly ,and you should know that i don't put } in these program.
For example this program-

#include<iostream>
using namespace std;
int main(){  
	int year ,month,day,leapyear;
	cout<<"Enter a year and month ";
	cin>>month>>year;
	if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))){
		leapyear=1;
	   cout<<"This is aleap year";
	   else{
	   cout<<"This is not leap year";
	   }
	}
return 0;
}

You need to put the } in the program. Every { must have a corresponding }. You have a starting bracket after the if condition, so you need an ending bracket right before the word "else".

#include<iostream>
using namespace std;
int main()
{  
	int year ,month,day,leapyear;
	cout<<"Enter a year and month ";
	cin>>month>>year;
	if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
        {
	   leapyear=1;
	   cout<<"This is aleap year";
        }   // add this
	else
        {
	   cout<<"This is not leap year";
	}
}  // delete this
        return 0;
}

Looks like you have an extra bracket on line 17 too. Delete it and place the } before "else".

Thank you so much,but i have problem with another prgram i will post it here.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.