Thread: switch case with dynamic

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    switch case with dynamic

    i am trying to use switch case with a variable and was wondering if some help could be offered

    error I get is: error C2051: case expression not constant

    I have the code:

    Code:
    for(int i = 0; i< vector1.end(); ++i)
      {
         int position = i;
         if(conditional)
         {break;
          }
          else{}
       }
    
    switch(position)
     {
       case 0:
       case vector1.end() :
       default:
    }
    how can I achieve the above?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not possible with a switch. You may try if and else.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    error I get is: error C2051: case expression not constant
    Yep, switch condition needs to be constant, vector.end() is not.
    Also, your switch statement is outside you for loop so position is out of scope and cannot be seen by the switch statement.
    Also, vector.end() returns an iterator so cannot be used in a for loop the way you are trying to.
    I believe your looking for vector.size();
    Spidey out!

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Also, I dont know if you know this but you dont need braces after every if else statement, you can just do this -

    Code:
    if(conditional)
       break;
    else
       ;
    its just better looking.
    Spidey out!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If the else is empty, why have an else at all? Every if does not require a corresponding else.

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by bithub View Post
    If the else is empty, why have an else at all? Every if does not require a corresponding else.
    Yes, that is true as well.
    Also, not sure if you left this out on purpose but every case needs a break as well otherwise it will fall through to the next one.
    Spidey out!

  7. #7
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Quote Originally Posted by Spidey View Post
    Yes, that is true as well.
    Also, not sure if you left this out on purpose but every case needs a break as well otherwise it will fall through to the next one.
    That is actually the main feature you lose out on when you cannot use a switch/case. You can do something like this though.

    Code:
    namespace Case
    {
      void moveup(void);
      void moveback(void);
      void moveleft(void);
      void moveright(void);
      void jump(void);
    }
    
    int position;
    
    #define CASE(condition, action)  if (position == condition) { action } else
    #define DEFAULT(action) action
    
    int main(void)
    {
      // code goes here...
    
      CASE(0, moveup(); jump();)
      CASE(vector1.end(), moveright();)
      DEFAULT(moveleft();movedown();)
    
      return 0;
    }
    Admittedly its not as intuitive to write, but it reads pretty clearly, yet uses a nasty global...
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's even more nasty because it uses macros. I would get rid of them in favor of the "manual" way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Cooloorful View Post
    That is actually the main feature you lose out on when you cannot use a switch/case. You can do something like this though.

    Code:
    namespace Case
    {
      void moveup(void);
      void moveback(void);
      void moveleft(void);
      void moveright(void);
      void jump(void);
    }
    
    int position;
    
    #define CASE(condition, action)  if (position == condition) { action } else
    #define DEFAULT(action) action
    
    int main(void)
    {
      // code goes here...
    
      CASE(0, moveup(); jump();)
      CASE(vector1.end(), moveright();)
      DEFAULT(moveleft();movedown();)
    
      return 0;
    }
    Admittedly its not as intuitive to write, but it reads pretty clearly, yet uses a nasty global...
    No, I would not recommend that approach at all.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    many thanks for the help. it is enough to use if, else if , else statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM