1,105,556 Community Members

Need help regarding switch statement in C

Member Avatar
chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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..

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
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
{}
}
Member Avatar
chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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??

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
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?

Member Avatar
chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
death_oclock
Posting Whiz
392 posts since Apr 2006
Reputation Points: 103 [?]
Q&As Helped to Solve: 45 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
karthi.k.s
Newbie Poster
9 posts since Jun 2011
Reputation Points: -7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
-1
 

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

Member Avatar
chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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: