1.11M Members

Need help regarding switch statement in C

 
0
 

Hello,
I was given a question a few few days back to write a program that lets a user enter a value for an angle and quadrant in which the angle lies is printed using switch statement.
I was able to make this program using if-else very easily but making it with switch statement is a problem..
I tried using

switch (angle)
{
      case (angle>=0 && angle<=90):
              printf("1st quadrant");
              break;
.
.
.
.
}

and so on....(where angle is the variable in which the value of angle is stored )
it gives me the error "Case constant required in statement switch"...
it works if I write it like case 90:,case 45: and all other constant values..
but,i can't go writing all 360 cases for all angles,can I???
So,is there any way to check a condition in switch statement??

---
Thanks in advance..

 
1
 

The error message tells it all. The case needs to be a constant...Like

1,2,3,4,5...or

#define one 1
const unsigned long two 2;

switch(angle)
{
case:1
{}
case:two
{}
}
 
0
 

dear gerard4143,
Thanks for the snappy reply.
I figured out what you are saying.But,the problem is that i can't go on writing all 360 cases for 360 values of an angle.
what I'm seeking is a solution for shortening the program length by somehow testing the condition in the case...
Is there any way of doing that??

 
0
 

But,the problem is that i can't go on writing all 360 cases for 360 values of an angle.
what I'm seeking is a solution for shortening the program length by somehow testing the condition in the case...
Is there any way of doing that??

that's where if else conditions shines, is it really required to use only a switch statement?

 
0
 

dear zeroliken,
that's where my problem started...
I have to use only switch statement..

 
1
 

There is a single arithmetic operation you can use to convert the ranges 0-89, 90-179, 180-269, 270-359 into the values 0, 1, 2, 3, respectively. The result represents the quadrant; 0 would be the first quadrant.

 
-1
 

you can if else ladder that will solve this problem in better way

 
0
 

There is a single arithmetic operation you can use to convert the ranges 0-89, 90-179, 180-269, 270-359 into the values 0, 1, 2, 3, respectively. The result represents the quadrant; 0 would be the first quadrant.

@death_oclock
Thank you very much...
it solved my problem...

Thanks Again...

Question Answered as of 2 Years Ago by death_oclock, gerard4143, karthi.k.s and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: