Hello, i have to create a program for-
If the age is less than 18-You are still a kid
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;
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'

Thank you.

``````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)
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

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.

Probably you could use a switch statement ????

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.