954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"illegal else without matching if"

while( i < lineCount) {
	current = 1;
		
		while (nList[i-1] = nList[i]&& i < lineCount) {

			current++;
			i++;

			if (current > highest);
				
			highest = current;
			arrayposition = i;

			multiMode = false;

		} else if(current == highest) {
		multiMode = true;
		}
		i = i + 1;
		}

	if (multiMode == true)

	return -1;

	else return arrayposition;


The error points to the line

} else if(current == highest) {

reading "illegal else without matching if"

I have just started learning C++ and would appreciate any help you can offer, it's probably a simple mistake :o

Talyen42
Newbie Poster
1 post since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

>reading "illegal else without matching if"
It's pretty straightforward. To have an else clause you first need an if clause. Since the closest match is a while loop, you've created a syntax error.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Ok.... I know you're new to this so I wont go to hard on ya....

There's a couple of problems going on here, for your error though, your code is trying to apply an else to a while loop which just isn't possible.

Formatting your code will help a lot, take a look:

while( i < lineCount) {
	current = 1;
	while (nList[i-1] = nList[i]&& i < lineCount) {
		current++;
		i++;
                // you had a semicolon(;) ending the following line, big oops
		if (current > highest){
			highest = current;
			arrayposition = i;
			multiMode = false;
		} else if(current == highest) {
		        multiMode = true;
		}
		i = i + 1;
	}

	if (multiMode == true){
        	return -1;
        }
	else {
                return arrayposition;
        }
}


That semicolon on the end of your if line basically ended the IF before it had done anything, therefore the code on the lines after it was being executed no matter what the conditions were. When it got to the else the compiler was litterally confused because it couldn't find the IF you were talking about. I didn't change the code much else besides that, just added some braces for clarity.

wally_lawless
Light Poster
26 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

I'd guess that you intended this:

if (current > highest);


to be this:

if (current > highest){
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You