We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,973 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Need help regarding switch statement in C

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..

5
Contributors
7
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
{}
}
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 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??

chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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

chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
Skill Endorsements: 0

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

karthi.k.s
Newbie Poster
9 posts since Jun 2011
Reputation Points: 3
Solved Threads: 2
Skill Endorsements: 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...

chirag_mittal
Newbie Poster
15 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by death_oclock, gerard4143, karthi.k.s and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1175 seconds using 2.92MB