cborland-c

C Borland Compiler Error


Back to basics with Borland C Compiler. I keep getting the following message and it is driving me crazy as I cannot find where the semi colon should go or why it is expected

>bcc32 -v- -w -O1 oddEven.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
oddEven.c:
Error E2378 oddEven.c 16: For statement missing ; in function main()
*** 1 errors in Compile ***
>Exit code: 1

Here is my code, Thanks for any help in advance

/* Program to check if a given integer between 1 and 100 is odd or even
 Date: 09/10/2015 */


#include <stdio.h>

main()
{
    int input;

    printf("\nPlease Enter A Number Between 1 and 100\n");
    scanf("%d", &input);
    printf("\nYou Entered %d\n", input);

    //for loop used for error handling
    for(input > 0 && input < 101)
    {

            if(input <= 0 && input > 100)

                {
                  printf("Error!! Please Enter A Number Between 1 and 100");

                 }//end if 1


            //modulo 2 to check for even divisibility 
            if(input % 2 == 0)
                {
                    printf("\n %d is EVEN", input);
                }//end if 2

            else
                {
                    printf("\n %d is ODD", input);
                }//end else

    }//end for loop

        getchar();
        getchar();

}//end main

Solution

  • This:

    for(input > 0 && input < 101)
    

    is invalid syntax. It should be

    while(input > 0 && input < 101)
    

    but then you have an infinite loop on entering anything valid. It should probably be an if, but then, no error message is printed when the user enters an invalid number. You should then move

    if(input <= 0 && input > 100)
    {
        printf("Error!! Please Enter A Number Between 1 and 100");
    }//end if 1
    

    outside the if.

    There are a lot of other problems as well. I suggest you read a good C book.

    Fixed Code:

    /* Note how this code is indented and looks clean */
    
    #include <stdio.h>
    
    int main(void) /* Valid signature of main */
    {
        int input, c; /* Note the extra variable */
    
        printf("\nPlease Enter A Number Between 1 and 100\n");
        scanf("%d", &input);
    
        printf("\nYou Entered %d\n", input);
    
        if(input <= 0 || input > 100) /* Note the use of || instead of && */
        {
            printf("Error!! Please Enter A Number Between 1 and 100 \n");
        }
        else /* Note the else */
        {
            if(input % 2 == 0)
            {
                printf("\n %d is EVEN \n", input);
            }
            else
            {
                printf("\n %d is ODD \n", input);
            }
        }
    
        while((c = getchar()) != '\n' && c != EOF); /* Clear the stdin */
        getchar(); /* Wait for a character */
    }