'else' without a previous 'if'
Solution 1
The problem is with your brackets. Indenting is important to understand where to open and close your brackets
int talet;
scanf("%d",&talet);
int i=1;
while(i<=99)
{
int a;
if (i%talet==0 || talet==(i/10)%10 ||talet==i%10)
{
if (talet%10==0)
printf("\n");
else
continue;
printf("burr ");
}
else
{
printf("%d ",i);
}
i=i+1;
}
Solution 2
Your problem is here:
}
printf("burr "); //<---
else
printf("%d ",i);
You can't have any statements before the else block. So remove it or move it inside the else OR if block, something like this:
} else {
printf("burr ");
printf("%d ",i);
}
Solution 3
The problem is that you have a printf
outside the if
brackets. Because of this, compiler thinks that the if
statement finished. When it reaches the else
, throws an error since there is no open if
condition
You should have this
if (i%talet==0 || talet==(i/10)%10 ||talet==i%10)
{
if (talet%10==0)
printf("\n");
else
continue;
printf("burr "); // <-- this was moved
}
else
printf("%d ",i);
Solution 4
try to keep your code-blocks as clean and readable as possible. This will prevent you from making mistakes.
You can write an if else Horstmann style:
if (condition)
{
#statements
}
else
{
#statements
}
or a bit more compact in TBS1 style:
if (condition) {
#statements
} else {
#statements
}
choose one you like, more styles in the comment provided by crashmstr (thanks to him), and stick to it. It WILL improve your code quality.
Diaco
Updated on October 10, 2020Comments
-
Diaco over 3 years
I am just beginning to learn C programming, so I am not really on an advanced level. Bear with me!
I have this piece of code, and I guess that it is pretty easy to understand what I am trying to do. However I get an error saying the last else is called without an if before it.
I suspect that the problem is my if-else statement inbetween the if and else. How would you guys solve it?
int talet; scanf("%d", &talet); int i = 1; while (i <= 99) { int a; { if (i % talet == 0 || talet == (i / 10) % 10 || talet == i % 10) { if (talet % 10 == 0) printf("\n"); else continue; } printf("burr "); else printf("%d ", i); } i = i + 1; }
-
Diaco over 9 yearsProblem is, I want that statement to be a part of the previous if-statement. I just dont want to repeat it in both the "if and else" of the "if", if you understand what I mean
-
Rizier123 over 9 years@diaco Then just put it in the previous if statement?!
-
Diaco over 9 yearsthat was my intetion and I thought it actually was inside it now. Notice that I have previously been learning Python. Maybe that is why I am misunderstanding it. So everything inside { } is part of the if statement in other words?
-
Rizier123 over 9 years@diaco Yes, if you have a code block (
{}
) with the if statement all inside of this is part if the if statement. e.g.if (condition) { //If statement} else { //else statement}
And if don't have the brackets it's only the next statement. e.g.if (condition) statement; else statement;
-
phil652 over 9 yearsI'm guessing this works for you, happy I could help you. Good luck!
-
crashmstr over 9 yearsI don't know that I would call the first example "oldschool" nor would I associate the second example exclusively with Java. Indent Style