Hello, i have to create a program for-
If the age is less than 18-You are still a kid
18-30 adult
30-60 middle age
greater than 60-senior citizen
else for any other impossible age (-ve or 0 age) ' wow '

Here is my attempt,

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();

int n;
printf("Please enter your age ");
scanf("%d" , &n);

if (0<n<18) printf("You are still a kid");
else if (18<=n<30) {printf("You are an adult");
        else if (30<=n<60) {printf("You are a middle-aged person");
                 else if (60<=n) {printf("You are a senior citizen");
                         else {printf("Wow !!!");
}
}
}
}
getch();

}

My compiler says 'Misplaced else'
Please help me find my mistake.

Thank you.

Dani AI

Generated

Two separate issues were hiding behind the syntax error: the chained comparisons and a few portability/usability choices. saw the formatting problems and the comparison bug; confirmed the code was being built with Turbo C 3.0 (an old, non‑portable environment); suggested a switch-based approach. The following adds a practical, modern C pattern that avoids those pitfalls and also adds input validation and clear boundary handling.

A robust pattern: isolate categorization in a function (or small enum), validate input with fgets+strtol (safer than unchecked scanf), avoid conio.h/clrscr/getch, and keep range tests simple (use if (age < 18), else if (age < 30), etc.). Note: 0 < n < 18 does not test “n between 0 and 18” in C — it is parsed as (0 < n) < 18, so the left comparison yields 0 or 1 and the whole expression is almost always true; use && or the chained if (n < 18) style shown below.

Example (compact, portable approach):

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

typedef enum { KID, ADULT, MIDDLE, SENIOR, INVALID } AgeCat;

AgeCat categorize(int age) {
    if (age <= 0 || age > 130) return INVALID;
    if (age < 18) return KID;
    if (age < 30) return ADULT;
    if (age < 60) return MIDDLE;
    return SENIOR;
}

/* main uses fgets + strtol to validate numeric input safely */

Notes and quick tips:

  • Test boundary values explicitly (0, 17, 18, 29, 30, 59, 60, negative and very large values).
  • Pick and document whether endpoints like 30 or 60 belong to the lower or upper category and be consistent.
  • Prefer a modern toolchain (gcc/clang with -std=c11 -Wall -Wextra) instead of Turbo C for correct language behavior and better diagnostics.
  • For user input robustness, prefer fgets + strtol over scanf to detect non-numeric input.

Recommended Answers

All 5 Replies

if (0<n<18) printf("You are still a kid");
else if (18<=n<30) {printf("You are an adult");
        else if (30<=n<60) {printf("You are a middle-aged person");
                 else if (60<=n) {printf("You are a senior citizen");
                         else {printf("Wow !!!");

You have placed braces before printf. Remove them.

EDIT:

You code would look more appealing(read: not Horrible) if you put it like this & it would also make some logical sense

if (0<n<18) 
   printf("You are still a kid");
else if (18<=n<30) 
   printf("You are an adult");
else if (30<=n<60) 
   printf("You are a middle-aged person");
else if (60<=n) 
   printf("You are a senior citizen");
else 
   printf("Wow !!!");

If you decide to go for if-else ladder make sure to format them neatly or the code logic does NOT come quick.

Removing braces or rewriting the code the way you mentioned doesn't help.
I get

[IMG][/IMG]


EDIT
I use Turbo 3.0 compiler by the way.

Didn't see 2 more things:

else if (18<=n<30) 
.
.
else if (30<=n<60) 
.
.

This is illegal. You have to use the Logical Operator AND(&&)

else if (18<=n && n<30) 
.
.
else if (30<=n && n<60) 
.
.

Problem solved,thank you.

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.