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.

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.

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.