开发者

I fail to make enum work correctly [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
enum ENU{YES=0,NO,DONTKNOW};

void func(ENU e)
{
   int n;
   cout<<"1+1=";
   cin >> n;
   if(n==2)
     cout<<e.YES;
   else 
     cout<<e.NO;
   if(ischar(n))   
     cout<<e.DONTKN开发者_高级运维OW;
}

The error is always displayed. Because my program is too small and poor formed.


cout<<e.YES;
cout<<e.NO;

e is a variable. When you do e.YES you are trying to depict that YES is a member of e; which is not correct. I think you wanted

cout<<YES;
cout<<NO;


Your use of enum is wrong! You can't write e.YES, you have to assign the value YES to variable 'e' in this way: e = YES and then you can display it. And the display will be wrong, the cout will display it as its int value so that YES will be displayed as 0 and NO will be displayed as 1.


You probably want something like this:

    enum ENU {YES=0,NO,DONTKNOW};
    void func()
    {
       int n;
       cout<<"1+1=";
       cin >> n;

       if(ischar(n))   
         cout<< DONTKNOW;
       else if(n==2)
         cout<< YES;
       else 
         cout<< NO;
    }

There's no point in your example in passing the parameter ENU to func(). Also, you must test for ischar() first, to avoid entering any of the two other tests if it's not necessary. Note that outputing your enum value with "cout<< YES;" for example, will not write "YES" to the console, but only its numeric value "0".

And a version which demonstrate the use of enum a little better:

void func()
{
   int n;
   cout<<"1+1=";
   cin >> n;
   ENU e = YES;

   if(ischar(n))   
     e = DONTKNOW;
   else if(n==2)
     e = YES;
   else 
     e = NO;

    cout << e << endl;
}

Remark: ischar() is probably not working as you would expect, as you're not giving it a character code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜