-1

I'm new to C language and would like to ask help on how to fix the said error on Line 15: error: expected expression before 'else'.

Thank You!

#include<stdio.h>

int n;

main()
{
    printf("Enter your age for qualification:");
    scanf("%d", &n);

    if (n >= 0 && n <= 100);
    {
        if (n <= 0);
        {
            printf("you're still in your mother's womb!");
        }
        else
        {
            (n >= 1 && n <= 17);
            printf("you're too young! you're not qualified!");
        }
        if (n >= 18 && n <= 100) {
            printf("you're qualified to vote");
        } else {
            (n >= 121);
            printf("that's impossible! you're already dead!");
        }
    }

    return 0;
}
dhh
  • 4,289
  • 8
  • 42
  • 59
  • 2
    `if(n<=0);` <== lose the semi-colon. I have no idea what you hope `(n>=1 && n<=17);` is accomplishing. [Perhaps a good book on the C language](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list?s=1|5.9428) may be helpful. – WhozCraig Jun 29 '16 at 07:42
  • Thank you for the suggestion! I'm trying to make a code for an age gate (eligibility to vote) the (n>=1 && n<=17); is to show the message ("you're too young! you're not qualified!") when someone inputted ages 1 - 17. Unless I coded it wrong though, which is likely. Thank you! :) – NeoArcadia Jun 29 '16 at 07:52
  • Please, bear in mind that 'semicolon' (`;`) can also be interpreted as an _empty statement_ ( if `n` is less or equal `0`, fine, we're done with condition checking ), so your `else` is left without its `if` that it depends on. `else` without `if` doesn't exist in a valid C statement. (-: – user3078414 Jun 29 '16 at 08:21

3 Answers3

1

When you do e.g.

if(n<=0);

you have a single if statement that does nothing. And then the else is a stray keyword out of context.

The format of an if statement is

if (condition) statement

or

if (condition) statement else statement

Note that there is no semicolon after the condition, instead it's part of statement.

In your case with if(n<=0); the semicolon at the end is the statement, an empty statement.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

To clear things out some more, your if statement would look something like this

if(n<=0)
    ;

That is an if with a blank line after that, hence nothing gets executed.

Haris
  • 12,120
  • 6
  • 43
  • 70
0
    #include<stdio.h>

    int n;
    main()
    {
        printf("Enter your age for qualification:");
        scanf("%d",&n);

      if (n>=0 && n<=100)
      {
        if (n<=0)
        {
            printf("you're still in your mother's womb!");
        }
        else
        {
            /*(n>=1 && n<=17);*/
            printf("you're too young! you're not qualified!");
        }
        if(n>=18 && n<=100)
        {
            printf("you're qualified to vote");
        }
        else
        {
             /*(n>=121);*/
             printf("that's impossible! you're already dead!");
        }
      }

      return 0;  
    }

What your code should looks like, for example

Destrif
  • 2,104
  • 1
  • 14
  • 22