CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    can anyone help me with this error message ?

    The following code yields an error message:
    Code:
    // temp1.cpp
     
    #include <iostream>
    using std::cout;
     
    void myM(char *);
     
    int main()
    {
       myM("june");
       return 0;
    }
    void myM(char *month)
    {
       switch(month)
       {
          case 'june':
             cout << "\nJune";
       }
    }
    
    after compilation i recieve an error message saying:
    error e2383 switch selection expression must be of integral type in function myM(char *)
    can anyone tell me how to change the code so it doesnt yield an error message ?
    Thanks !

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: can anyone help me with this error message ?

    it should work this way
    Code:
    #include <iostream>
    
    enum month {
       jan, feb, mar // ....
    };  
    
    void myM(month m);
     
    int main()
    {
       myM(jan);
       return 0;
    }
    void myM(month m)
    {
       switch(m)
       {
          case jan:
             std::cout << "\nJanuary";
       }
    }
    K.

  3. #3
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    thanks

    But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: thanks

    Quote Originally Posted by PsSheba
    But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?
    you cannot do what you want
    as the compiler tells you
    Code:
    switch selection expression must be of integral type in function myM(char *)
    first mistake:
    Code:
       switch(month) // month is a pointer
    second mistake:
    Code:
          case 'june': // 'june' is not a valid construct in C/C++
    the closest you can get is something like
    Code:
    void myM(char *month)
    {
       std::string m(month);
       if ( m == "june" )
          cout << "\nJune";
       else if ( m == "april" )
          cout << "\nApril";
       }
    }
    K

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: thanks

    Quote Originally Posted by PsSheba
    But i'd rather stick to my code. The example i sent is only a part of a larger code and i cannot change it. Any idea whats wrong with the code i sent ?
    The 'switch' statement can only work on expressions which are of integral type or of a class type for which there is an unambiguous conversion to integral type which isn't the case in your example...

  6. #6
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Thanks a lot !

    Now the picture is clear !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured