enum fuzzy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amanda22
    New Member
    • Nov 2007
    • 8

    #1

    enum fuzzy

    I have the following question I am working on, and I'm not sure what they are asking.

    Write a C statement which defines an enumeration type fuzzy consisting of values false, maybe, and true . Defined so that maybe is greater in value than false but less than true .
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    So do you know how to declare an enum?

    Comment

    • beacon
      Contributor
      • Aug 2007
      • 579

      #3
      Originally posted by amanda22
      I have the following question I am working on, and I'm not sure what they are asking.

      Write a C statement which defines an enumeration type fuzzy consisting of values false, maybe, and true . Defined so that maybe is greater in value than false but less than true .
      enum changes the values of words and assigns a number to them. For instance, if you create an enumerated variable Amanda and you wanted to to know if she is a girl you could enumerate yes and no.

      Your code would look like this:
      Code:
      enum Amanda(yes, no)
      where 'yes' would be given the value of 0 and 'no' would be given the value of 1. Any subsequent words placed in the parenthesis would increment from 1.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by beacon
        For instance, if you create an enumerated variable Amanda and you wanted to to know if she is a girl you could enumerate yes and no.

        Your code would look like this:

        Code: ( text )
        enum Amanda(yes, no)
        Your code example won't compile.

        First you declare an enum:
        [code=c]
        enum Status { yes, no};
        [/code]

        Then you create an integer variable, like Amanda. After that you can use the enum.
        [code=c]
        int Amanda ;
        //etc... value gets put into Amanda
        //then:
        if (Amanda == yes)
        {
        printf("She does\n");
        }
        else
        {
        printf("She doesn't\n");
        }
        [/code]

        Comment

        • beacon
          Contributor
          • Aug 2007
          • 579

          #5
          Oops...I took it upon myself to try and learn enums after you posted something about it on one of my posts weakness and this was my opportunity to put what I had learned to work.

          So where you put enum Status, does that replace the variable Amanda that's declared? Or would you declare it like I did, but then test it to determine if it does or doesn't?

          I was thinking that you would have to test, but you could say
          Code:
          if (Amanda == 0)
               cout<<"She does"<<endl;
          else
               cout<<"She doesn't"<<endl;
          Is it just all about how you decide to implement it?

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Enum declarations should go at the top, by function definitions or global variables, to ensure they are global in scope.

            An old use for enums was enum boolean{FALSE, TRUE}, since no builtin boolean type existed in C before C99.

            This makes FALSE and 0 equivalent and TRUE and 1 equivalent, so that instead of having to use 0 and 1 for, say, an isEmpty() function, you can simply use the more readable (and less likely to cause programmer error) enum.

            Thus,
            Code:
            enum boolean {FALSE, TRUE};
            
            SomeStructure::boolean isEmpty(){
              if (size == 0)
                  return TRUE;
              return FALSE;
            }
            is equivalent to:

            Code:
            SomeStructure::int isEmpty(){
               if (size == 0)
                 return 1;
               return 0;
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by beacon
              So where you put enum Status, does that replace the variable Amanda that's declared? Or would you declare it like I did, but then test it to determine if it does or doesn't?
              An enum is a list of named integer values. It is a declaration, not a definition. Because it's a declaration, you can out your enums in header files.

              [code=c]
              enum Status { yes, no};
              [/code]

              This just declares that whn I use yes in the code, I mean 0 and when I use no I mean 1. The enum avoids hard-coded values in your programs.

              So you code:
              [code=c]
              if (Amanda == yes)
              cout<<"She does"<<endl;
              else
              cout<<"She doesn't"<<endl;
              [/code]

              Rather than:

              [code=c]
              if (Amanda == 0)
              cout<<"She does"<<endl;
              else
              cout<<"She doesn't"<<endl;
              [/code]

              If you have that 0 hard-coded in 500 places and you have to change it to a 2 for some reason, you have to make 500 changes. With the enum, you just change the enum and recompile.

              Originally posted by Laharl
              Enum declarations should go at the top, by function definitions or global variables, to ensure they are global in scope.

              An old use for enums was enum boolean{FALSE, TRUE}, since no builtin boolean type existed in C before C99.

              This makes FALSE and 0 equivalent and TRUE and 1 equivalent, so that instead of having to use 0 and 1 for, say, an isEmpty() function, you can simply use the more readable (and less likely to cause programmer error) enum.

              Thus,

              Code: ( text )
              enum boolean {FALSE, TRUE};

              SomeStructure:: boolean isEmpty(){
              if (size == 0)
              return TRUE;
              return FALSE;
              }
              There are some fallacies here.

              One, in C, TRUE and FALSE are usually not enums but are #define macros.

              Two, function defintiions and global variables do not go in header files, but enums do. Putting function definitions and global variables at the top of a source file is the old C practice used when the entire program was in one file. C++ always uses multiple files.

              Three, there is no use for a global variable. I have just written an article about this that should appear in the C/C++ Articles forum shortly.

              Four, you do not use TRue and FALSE in C++, and you example was a C++ example. In C++ you use the keywords true and false and never mind what the valus is.

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                The example was taken from an old (read: pre-1999) C++ book, along with the rest of my understanding of enums. I don't actually know C directly, so I had to improvise based on the limited examples available in the book.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Keep posting. You probably know a lot more that you are letting on.

                  Comment

                  Working...