"illegal else without matching if"

Pages: 12
I got this error multiple times in a program I wrote and I don't know why...It is only my second program any help would be appreaciated. It's kind of a weird program but its for my c++ class...heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;

void main()
{
	char color, solid, length;
	cout<<"Please answer the questions with the first character of the word response, (ex: 't' or 'T' for tan, 'l' or 'L' for long, etc.).\n";
	cout<<"What is the fur length of the cat? (l, s, m)";
	cin>>length;
	if (length=='l')
	else if (length=='L')
	{
	cout<<"What is the major color of the cat? (t, b, o) ";
	cin>>color;
		switch (color){
			case 't':
			case 'T':	cout<<"This cat is named Twinkie.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Cornelius.";
							break;
	}
	else if (length=='s')
	else if (length=='S')
	{
		cout<<"What is the major color of the cat? (t, b, o) ";
		cin>>color;
		switch (color){
			case 't':
			case 'T':	cout<<"This cat is named Princess.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Dudley.";
							break;
			case 'b':
			case 'B':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y')
						else if (color=='Y')	cout<<"This cat is named Nicodemus.";
													break;
						else if (color=='n')
						else if (color=='N')	cout<<"This cat is named Jimmy.";
													break;
	}
	else if (length=='m')
	else if (length=='M')
	{
		cout<<"What is the major color of the cat? (b, o, t)";
		cin>>color;
		switch (color){
			case 'b':
			case 'B':	cout<<"This cat is named Panther.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Ivan.";
							break;
			case 't':
			case 'T':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y')
						else if (color=='Y'){
							cout<<"This cat is named Priss";
								break;
						else cout<<"This cat is named Bucky";
						}
	}
}


Last edited on
Your post is so badly formatted that I doubt anyone is going to try to decipher it. Your error message means that you have an "else" statement without a matching "if" statement. You'll have to go through your own code and find out where that is.
1
2
if (length=='l')
else if (length=='L')
Is wrong -as all other else if() else if() groups.
Use if ( length == 'l' || length == 'L' ) || means or
Ok, so I fixed that problem, thank you for your help, but I still have 4 more errors with the same message....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

void main()
{
	char color, solid, length;
	cout<<"Please answer the questions with the first character of the word response, (ex: 't' or 'T' for tan, 'l' or 'L' for long, etc.).\n";
	cout<<"What is the fur length of the cat? (l, s, m)";
	cin>>length;
	if (length=='l' || 'L')
	{
	cout<<"What is the major color of the cat? (t, b, o) ";
	cin>>color;
		switch (color){
			case 't':
			case 'T':	cout<<"This cat is named Twinkie.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Cornelius.";
							break;
	}
	else if (length=='s' || 'S')
	{
		cout<<"What is the major color of the cat? (t, b, o) ";
		cin>>color;
		switch (color){
			case 't':
			case 'T':	cout<<"This cat is named Princess.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Dudley.";
							break;
			case 'b':
			case 'B':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y' || 'Y')			cout<<"This cat is named Nicodemus.";
															break;
						else if (color=='n' || 'N')		cout<<"This cat is named Jimmy.";
															break;
	}
	else if (length=='m' || 'M')
	{
		cout<<"What is the major color of the cat? (b, o, t)";
		cin>>color;
		switch (color){
			case 'b':
			case 'B':	cout<<"This cat is named Panther.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Ivan.";
							break;
			case 't':
			case 'T':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y' || 'Y')
						{
							cout<<"This cat is named Priss";
								break;
						else cout<<"This cat is named Bucky";
						}
	}
}
Check your braces, you are missing some
And use int main() instead of void main().
Thank you!....I have 2 more errors I don't think they are braces but I could be wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

void main()
{
	char color, solid, length;
	cout<<"Please answer the questions with the first character of the word response, (ex: 't' or 'T' for tan, 'l' or 'L' for long, etc.).\n";
	cout<<"What is the fur length of the cat? (l, s, m)";
	cin>>length;
	if (length=='l' || 'L')
	{
	cout<<"What is the major color of the cat? (t, b, o) ";
	cin>>color;
		switch (color)	{
			case 't':
			case 'T':	cout<<"This cat is named Twinkie.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Cornelius.";
							break;
						}
	}
	else if (length=='s' || 'S')
	{
		cout<<"What is the major color of the cat? (t, b, o) ";
		cin>>color;
		switch (color)	{
			case 't':
			case 'T':	cout<<"This cat is named Princess.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Dudley.";
							break;
			case 'b':
			case 'B':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y' || 'Y')	{			cout<<"This cat is named Nicodemus.";
																break;
						else if (color=='n' || 'N')			cout<<"This cat is named Jimmy.";
																break;
												}
						}
	}
	else if (length=='m' || 'M')
	{
		cout<<"What is the major color of the cat? (b, o, t)";
		cin>>color;
		switch (color)	{
			case 'b':
			case 'B':	cout<<"This cat is named Panther.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Ivan.";
							break;
			case 't':
			case 'T':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y' || 'Y')
						{
							cout<<"This cat is named Priss";
								break;
						else cout<<"This cat is named Bucky";
						}
						}
	}
}
It says they are at lines 62 and 39
Check carefully this part of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	else if (length=='s' || 'S')
	{
		cout<<"What is the major color of the cat? (t, b, o) ";
		cin>>color;
		switch (color)	{
			case 't':
			case 'T':	cout<<"This cat is named Princess.";
							break;
			case 'o':
			case 'O':	cout<<"This cat is named Dudley.";
							break;
			case 'b':
			case 'B':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if (color=='y' || 'Y')	{			cout<<"This cat is named Nicodemus.";
																break;
						else if (color=='n' || 'N')			cout<<"This cat is named Jimmy.";
																break;
												}
						}
	}
I apologize, however, I do not see my error
Let me simplify it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ( ... )
{
    //...
    switch(...)
    {
          case ...:
                if ( ... )
                {
                    //...
                   else if ( ... )
                    //...
               }
    }
}
I'm still confused as to where it went wrong... its obviously there somewhere but I cant seem to find the error....there is one on the line of the "else if" I just don't know how to fix it...
Thank you for all of your help, I have to go but I will be checking back later to continue to try and resolve the last 2 errors I have....Thanks again!
All of your statements like this are wrong...

if (color=='y' || 'Y')

They should be...

if(color=='y' || color=='Y")

Note carefully the difference. Changle all occurrences of this type of error and you should be good to go.
I still have the issue of the illegal else....there is only two....one in line 62 and one in 39....i still have not resolved that error yet
Both of those are EXACTLY what I just told you about.
Also, for the love of god, fix your indentation >_> It would REALLY help you find these errors...
Thank you for your time....I still have the same error message on the same lines even with these corrections
The errors may not be on the exact line that the compiler says. You'll need to repost that SECTION of your code, not the entire function, and try to format it so we can read it. Also, in this section of your code, look at the braces... they are not correct....

1
2
3
4
5
6
7
8
9
10
cout<<"Is this cat a solid color? (y/n)";
                 cin>>color;
                 if (color=='y' || 'Y')
                 {
                      cout<<"This cat is named Priss";
                      break;
                 
                 else cout<<"This cat is named Bucky";
                 }
}


Your braces are ALL screwed up. Notice also that I've taken the time to try to put this section of your code into at least a passable format. If you really want help, you should exhibit the same courtesy.
Last edited on
Both of these sections have that error


1
2
3
4
5
6
7
8
9
10
			case 'B':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if(color=='y' || color=='Y')
						{
							cout<<"This cat is named Nicodemus.";
								break;
						else if(color=='n' || color=='N')			
							cout<<"This cat is named Jimmy.";
								break;
						}







1
2
3
4
5
6
7
8
9
10
			case 'T':	cout<<"Is this cat a solid color? (y/n)";
						cin>>color;
						if(color=='y' || color=='Y')
						{
							cout<<"This cat is named Priss";
								break;
						else
							cout<<"This cat is named Princess";
								break;
						}
Pages: 12