Not seeing the problem with my if statement

Jun 20, 2013 at 10:04pm
Hello everybody,

I am working on an assignment and have just started but now I am having an issue with my if statements. I keep getting error "C2181: illegal else without matching if" for lines 11 and 14. Could someone correct the syntax error I am making? I keep trying to figure it out but I am not having any luck. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{
	int UserNum=0;
	printf("Please enter a number 1-50: ");
	scanf(" %d", &UserNum);
	if (UserNum<1)
		printf("Please enter a number 1-50: ");
		scanf(" %d", &UserNum);
	else if (UserNum>50)
		printf("Please enter a number 1-50: ");
		scanf(" %d", &UserNum);
	else
		printf("Thank you.");
}


Edit: Also, in the compiler it has the red squiggly line under it indicating an error and it says it is expecting a statement for the elses in lines 11 and 14.
Last edited on Jun 20, 2013 at 10:05pm
Jun 20, 2013 at 10:10pm
whitespace is free, if you want several statements inside your if, then you make a block with braces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
	int UserNum=0;
	printf("Please enter a number 1-50: ");
	scanf(" %d", &UserNum);
	if (UserNum<1){ //opening block
		printf("Please enter a number 1-50: ");
		scanf(" %d", &UserNum);
	} //closing block
	else if (UserNum>50){ //same here
		printf("Please enter a number 1-50: ");
		scanf(" %d", &UserNum);
	}
	else
		printf("Thank you."); //just one statement, so there is no need to create a block for it
}
Jun 20, 2013 at 10:11pm
You need to enclose the statements controlled by the if using braces {}.
Without them, just a single statement belongs to the if or else

Like this, (same code, still incorrect), but indentation changed to show what happens:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    int UserNum=0;
    printf("Please enter a number 1-50: ");
    scanf(" %d", &UserNum);
    
    if (UserNum<1)
        printf("Please enter a number 1-50: ");
        
    scanf(" %d", &UserNum);
    else if (UserNum>50)
        printf("Please enter a number 1-50: ");
        
    scanf(" %d", &UserNum);
    else
        printf("Thank you.");
}

Jun 20, 2013 at 10:14pm
Indentation doesn't matter to the compiler. The compiler sees that code like this:

1
2
3
4
5
6
7
8
	if (UserNum<1)
		printf("Please enter a number 1-50: ");
	scanf(" %d", &UserNum);     // <- note that this is not part of the 'if' block
	else if (UserNum>50)    // <- therefore this 'else' does not have an 'if' before it
		printf("Please enter a number 1-50: ");
	scanf(" %d", &UserNum);  // <-  also this is not part of the 'else' block
	else   // <- so this also does not have an 'if' before it
		printf("Thank you.");


control statements like if/else/for/while etc go up until the next semicolon (;), or through the following {curly braces}. Since you have multiple statements you want in those blocks, you need to add {braces} to what you want to be inside the conditional:

1
2
3
4
5
6
	if (UserNum<1)
	{   // <- the brace here indicates everything after this is part of the 'if'
		printf("Please enter a number 1-50: ");
		scanf(" %d", &UserNum);
	} // <- the 'if' ends here
	else if (UserNum > 50) // <- now this will work because there's an if immediately before it 


EDIT: double ninja'd! doh!
Last edited on Jun 20, 2013 at 10:14pm
Jun 20, 2013 at 10:14pm
Thank you both for your insight. I am new so I am still making more than my fair share of mistakes. Thanks again!

Edit: Thanks to you, too, Disch!
Last edited on Jun 20, 2013 at 10:27pm
Topic archived. No new replies allowed.