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;
}

Dani AI

Generated

The compiler message about an unmatched else means the else token is not paired with an if the parser considers still open. has already pointed to a misplaced brace in the posted snippet; this is the most common cause, but several other, subtler problems can produce the same error.

Common causes and quick checks

  • An extra closing brace earlier in the function can close the if scope before the else.
  • A stray semicolon after the if condition (if (cond);) makes the if have an empty statement, so the following else has no matching if.
  • Nested if statements without braces can lead to the “dangling else” binding to the wrong if.
  • Macros or preprocessor expansions that inject braces or semicolons can change block structure unexpectedly.

Practical diagnosis steps

  • Use an editor or IDE with brace/paren matching to jump between { and }.
  • Enable strong compiler diagnostics (for example, g++ with -Wall -Wextra or Clang) to get clearer locations for syntax problems.
  • Reduce the source to a minimal snippet that still reproduces the error; this often exposes the misplaced brace or stray token.
  • Run a static checker (for example, cppcheck) or format the file with clang-format to reveal structural problems.

Prevention and best practices

  • Always use braces for both if and else blocks, even for single statements (if (cond) { ... } else { ... }).
  • Keep nested logic small and well-indented so block boundaries are obvious.
  • Review any macros that wrap control flow.

Relevant references: C++ if statement details on cppreference (https://en.cppreference.com/w/cpp/language/if) and the dangling-else description on Wikipedia (https://en.wikipedia.org/wiki/Dangling_else).

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.