Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to figure this out I have to have three range 1-999 1000-1999 greater then 2000 for this program I'm trying to write
Posted
Comments
Andreas Gieriet 22-Oct-15 10:29am    
What with smaller than 1? What with equal 2000?
Regards
Andi
playaslaya 22-Oct-15 10:40am    
I'm sorry if there is language barrier I mean trying to find a way to use a range of numbers instead case 1 case 2 case 3. I have to figure a way to approach a assignment using switch but the assignment is asking for 1 to 999 for one equation and 1000 to 1999 for the second equation and 2000 to infinity for the third equation
jeron1 22-Oct-15 10:31am    
Sounds to me like if-then-else would be more fitting than a switch-case statement where there are only three conditions.
e.g. if ((x >= 1) && (x <= 999))
{
//do something
}
else if ((x > = 1000) && (x <= 1999))
{
// something else
}...........
playaslaya 22-Oct-15 10:36am    
Yeah that is a great solution but my instructor wants switch which is racking my brain I'm not looking for a complete solution just a idea to approach it
jeron1 22-Oct-15 10:47am    
How about using if-else to set an integer to a value that's unique for each range of numbers, then use a switch.

C++
switchValue = value / 1000;
switch (switchValue)
{
case 0: // 1 - 999
    break;
case 1: // 1000 - 1999
    break;
default: // > 1999
    break;
}

You may also need to add some logic for value being zero or less at the beginning.
 
Share this answer
 
Comments
Matt T Heffron 22-Oct-15 12:06pm    
+5
Richard MacCutchan 22-Oct-15 12:10pm    
:thumbsup:
If you have consecutive ranges, the most robust approach is to "step down the stairs" instead of giving ranges which might end up in subtle mistakes. E.g. assuming you have the ranges ...0, 1...999, 1000...1999, 2000..., a possible solution might be from top-down:
C++
if (n >= 2000) {
  // 2000...
} else if (n >= 1000) {
  // 1000...1999
} else if (n >= 1) {
  // 1...999
} else {
  // ...0
}
[EDIT]
Now that you mention your instructor wants switch (e.g. for didactic reason), you might calculate a range indicator and use that in the switch. E.g.
int lower_bound = n >= 2000 ? 2000
                : n >= 1000 ? 1000
                : n >=    1 ?    1
                : 0
                ;
switch(lower_bound)
{
case 2000:
  // 2000...
  break;
case 1000:
  // 1000...1999
  break;
case 1:
  // 1...999
  break;
default:
  assert(!"out-of-range");
  break;
}

[/EDIT]

Regards
Andi
 
Share this answer
 
v2
Comments
Matt T Heffron 22-Oct-15 12:05pm    
+5
Andreas Gieriet 22-Oct-15 15:23pm    
Thanks for your 5!
Cheers
Andi
If your instructor really really wants a switch, then create something like:

C++
int switchVal;
if(n<1)
  switchVal= 1;
if(n>1 && n<100)
  switchVal= 2;
... 
etc.

Now you can use

C++
switch(switchVal) {
  case 1: whatever;
         break;
   (more etc.)     
} 


and make him/her happy.
 
Share this answer
 
this is no task for a switch, but the if/else commands

C++
if( n >= 1|| n <= 999 ) {
} else if( n >= 1000|| n <= 1999 ) {
} else if( n >= 2000 ) {
} 


watch out for the ">=" equalities.

I think that in Swift from Apple you can write stuff like this

C++
switch( n ) {
  case 1...999:
    break;
}
 
Share this answer
 
Comments
Andreas Gieriet 22-Oct-15 10:42am    
You have the conditions wrong! It should be && and not ||. See also my solution #2 to avoid such subtle mistakes.
E.g. your first if matches any number greater than or equal 1, like 10000. The other ifs will not be reached...
Regards
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900