An 'illegal else without matching if'

I'm writing a program that uses if loops and if logical operators to evaluate whether a number is odd or even, but the compiler keeps bringing up the error in the title - the if statement is there I can see it lol!
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
/* Program to decide whether the numbers 1 .. n are odd/even */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
  double n, r;
  int t;
  char i [256];
/* User enters a value for n from stdin, and converted and stored as double r */

  printf("This is a program will decide whether number 1 .. n are odd or even.\n Enter the value of n :");
  gets( i );
  n = atof ( i );

/* Iteration to evaluate values between 1 .. n, and print whether they are odd or even*/
  if ( t = 1, t <= n, t++)
  {
	  printf("NUMBER			ODD/EVEN\n");   
	  r = fmod (t,2.0);

/* If the modulus of n / 2 == 0, then it must be even, if the test t==0 evaluates falsly it must be odd*/
	 if ( r == 0.0); {
	 printf("  %i				EVEN\n", n);
	 } else {
	 printf("  %i				ODD\n", n);
	 }
  }
  printf("The program is complete!\n");
}


Thanks in advanced for sharing your thoughts!
Last edited on
closed account (z05DSL3A)
if ( r == 0.0); { delete the ;
closed account (S6k9GNh0)
I suggest you use [code] [ /code] next time.
Thanks for the 2 pieces of advice.

The program is now compiling, but for some reason it's not doing it's desired job. I'll have a good look at it.
closed account (z05DSL3A)
should line 19 be for(...) not if(...)?
Last edited on
I just spotted that and the ; seperators in the for argument.

Thanks chap!
For completeness, and to help beginners like me, here's the final program.

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

/* Program to decide whether the numbers 1 .. n are odd/even */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
  double n, r;
  int t;
  char i [256];
/* User enters a value for n from stdin, and converted and stored as double r */

  printf("This is a program will decide whether number 1 .. n are odd or even.\n Enter the value of n :");
  gets( i );
  n = atof ( i );

/* Iteration to evaluate values between 1 .. n, and print whether they are odd or even*/
  printf("NUMBER			ODD/EVEN\n");
  for ( t = 1; t <= n; t++)
  {  
	  r = fmod (t,2.0);

/* If the modulus of n / 2 == 0, then it must be even, if the test t==0 evaluates falsly it must be odd*/
	 if ( r == 0) {
	 printf("  %i				EVEN\n",t);
	 } else {
	 printf("  %i				ODD\n",t);
	 }
  }
  printf("The program is complete!\n");
}
closed account (S6k9GNh0)
For a heads up, the math class isn't needed. Using modulo by doing something similar to n%2 will return a 1 or 0, 1 meaning it's odd and 0 meaning it's even.
Last edited on
Also, use int main().

And don't use gets()...it is easy to corrupt your memory with that. Use scanf() with %#s.
Last edited on
I tried % , I removed it early on in the debugging stage because I thought the problem had something to do with that operation, but it wasn't - so I'll use % in the future.

Firedraco could you explain your post please, I'm very new to C .

This forum is excellent, so many knowledgeable people - I'll be glad when I can start answering questions! Thanks again!
Topic archived. No new replies allowed.