CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2005
    Posts
    21

    Cool Why enum works so strange? :/

    look, this program print:
    1 640x480
    2 800x640
    3 1600x1200

    I thought I would get:
    low 640x480
    medium 800x640
    hight 1600x1200

    Why it print numbers?

    I am new at this forum and don't know how to use tags for code. Where is it?


    This is program:

    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    using namespace std;

    enum resolution {low,medium,hight};


    //class
    class display{
    int width,height;
    resolution res;
    public:
    void set(int w,int h){width=w; height=h;}
    void get(int &w,int &h){w=width; h=height;}
    void setR(resolution r){res=r;}
    resolution getR(){return res;}
    };

    //array
    char names[3][7]= {"low", "medium" , "hight"};

    int main(){
    display dm[3];
    int i,w,h;

    dm[0].set(640,480);
    dm[1].set(800,600);
    dm[2].set(1600,1200);

    dm[0].setR(low);
    dm[1].setR(medium);
    dm[2].setR(hight);

    cout<<"We have:"<<endl;

    for(int i=0;i<3;i++){
    dm[i].get(w,h);
    cout<<dm[i].getR()<<" "<<w<<"x"<<h<<endl;
    }
    return 0;
    }


    P.S. Need your advice.

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: Why enum works so strange? :/

    That's how enums are supposed to behave. When you declare an enum like so:
    Code:
    enum resolution {low, medium, hight};
    then low, medium, and hight are not string literals, as it seems you were expecting. Rather, they are the names of numeric constants that form the set of valid values for a variable of this enum type. For example, if you wrote something like this somewhere in your code:
    Code:
    enum Example {a, b, c};
    int a;
    you'd get an error, because you're trying to define a twice. If you don't define the numeric value of each constant yourself, then by default the first constant should be 0, and each one listed thereafter should have a value one greater than the one before it. When I compiled and ran your program, I got this output:
    Code:
    0 640x480
    1 800x640
    2 1600x1200
    which is what I would have expected. How you got 1, 2, and 3 in the first column, I'm not sure. A description of how to declare and use enums is given in MSDN, here.

    To format your code as you see in this post, wrap it in the [code]...[/code] tags. For example, writing this in your post:

    [code]int a;[/code]

    should produce this:
    Code:
    int a;

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why enum works so strange? :/

    If you want the names:

    Code:
    const char* resolution_name[] = { "low", "medium", "high" };
    int width, height;
    dm[res].get( width, height );
    std::cout << resolution_name[res] << ": " << width << 'x' << height << std::endl;

  4. #4
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Why enum works so strange? :/

    Another approach is using map of enumeration and string:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <map>
    #include <string>
    
    
    enum resolution {low,medium,hight};
    std::map<resolution,std::string > resMap;
    
    
         //class
        class display{
        int width,height;
        resolution res;
        public:
        void set(int w,int h){width=w; height=h;}
        void get(int &w,int &h){w=width; h=height;}
        void setR(resolution r){res=r;}
        std::string getR(){ return resMap[res];}
             
        };
    
    int main(int argc, char *argv[])
    {
       
       resMap[low]= "low"; 
       resMap[medium]= "medium";
       resMap[hight]= "high";
    
        display dm[3];
        int i,w,h;
        
        dm[0].set(640,480);
        dm[1].set(800,600);
        dm[2].set(1600,1200);
        
        dm[0].setR(low);
        dm[1].setR(medium);
        dm[2].setR(hight);
        
        std::cout<<"We have:"<<std::endl;
        
        for(int i=0;i<3;i++){
        dm[i].get(w,h);
        std::cout<<dm[i].getR()<<" "<<w<<"x"<<h<<std::endl;
    } 
          
        system("PAUSE");
        return 0;
    }
    Last edited by Guysl; September 19th, 2005 at 12:05 PM.
    **** **** **** **** **/**

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