Creating variables in switches

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    Creating variables in switches

    I am trying to create a new variable in a switch, read in some data then push back the variable onto a vector. The only problem is if I try to create a variable in a switch the compiler complains, so is there a way to do this without making the compiler complain?

    Example:
    Code:
    struct Fruit {
       std::string name;
       double price;
    };
    
    void Read(std::vector<Fruit>& f, std::ifstream& in) {
       int type;
       in >> type;
    
       switch(type) {
          case APPLE:
             Fruit apple;
             apple.name = "Apple";
             in >> apple.price;
             f.push_back(apple);
          
          case KIWI:
             Fruit kiwi;
             kiwi.name = "Kiwi";
             in >> kiwi.price;
             f.push_back(kiwi);
       }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Are you sure it's the object creation your compiler is complaining about?

    Strings are not integer values, and so they can't be used in switch statements. Better modify your input there, or just use if...else if...else statements.

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      It is saying:
      initialization of 'apple' is skipped by 'default' label

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        It is best to avoid creating variables on the stack inside switches for just this reason. The switch is a single block of code (unlike an if statement) so it is possible for the code creating the data (and executing the constructor) to be skipped by going through a different case statement.

        You can
        1. Add otherwise unrequired braces round the contents of the case
          Code:
                 case KIWI:
                    {
                    Fruit kiwi;
                    kiwi.name = "Kiwi";
                    in >> kiwi.price;
                    f.push_back(kiwi);
                    }
          artifically creating a code block
        2. Declare the variable outside the switch statement
        3. Create the data on the heap (use new and delete)



        BTW all you case blocks are missing break; statements.

        Comment

        • MrPickle
          New Member
          • Jul 2008
          • 100

          #5
          When I create an artificial code block, do I need to break the switch?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Yes, C++ is one of those languages that never invisibly adds statements for you, if you don't put in a break it wont break.

            Comment

            • MrPickle
              New Member
              • Jul 2008
              • 100

              #7
              Ok, thank you. It is working now.

              Comment

              Working...