I am using Visual C++ 6.0 and I am trying to use an if else statment but unable to resolve error. Here is the code:

/*--------------------------------------------------------*/
/*      Program Homework 3_3                              */
/*                                                        */
/*      This program computes the roots of a			  */
/*		2nd degree polonomial							  */

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

int main (void)

{
        /*      Declare variables                         */

        double a, b, c, x;


        /*      Inform and request info from user to	  */
		/*		preform If else function.				  */

		// restart_a

        printf("Please enter a value for a" 
			"inbetween 1 & 2: \n");
        
		scanf("%lf",&a);
		

		if (a >= 1 && a <= 2);

			printf("Thanks for choosing a valid character. \n");
						
		else  
			
			printf("Invalid entry! \n");
		
		// restart_a
			
		printf("a: %8.3f \n",a);
        
		/*      Exit program.                             */

return 0;

}

/*--------------------------------------------------------*/

The error that I am getting is:

--------------------Configuration: HomeW3_3 - Win32 Debug--------------------
Compiling...
HomeW3_3.c
\homew3_3.c(33) : error C2181: illegal else without matching if
Error executing cl.exe.

HomeW3_3.exe - 1 error(s), 0 warning(s)


Thanks for the help.


/imest

get rid of that silly semicolon ';' at the end of the if statement. The next line, print(...) need a semicolon at the end.

your if is oviously wrong.. you need to remove the semicolon... that is terminating the statement and not seeing your if clause...

try this

if (a >= 1 && a <= 2)
    printf("Thanks for choosing a valid character. \n");
else  
    printf("Invalid entry! \n");

or just use good ole {} for your if/else's
hth

Thank you very much for the help guys! Very much appreciated!

/imest

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.