473,480 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

enum fuzzy

8 New Member
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 .
Nov 13 '07 #1
8 2116
sicarie
4,677 Recognized Expert Moderator Specialist
So do you know how to declare an enum?
Nov 13 '07 #2
beacon
579 Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 13 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:
Expand|Select|Wrap|Line Numbers
  1. enum Status { yes, no};
  2.  
Then you create an integer variable, like Amanda. After that you can use the enum.
Expand|Select|Wrap|Line Numbers
  1. int Amanda ;
  2. //etc... value gets put into Amanda
  3. //then:
  4. if (Amanda == yes)
  5. {
  6.    printf("She does\n");
  7. }
  8. else
  9. {
  10. printf("She doesn't\n");
  11. }
  12.  
Nov 14 '07 #4
beacon
579 Contributor
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
Expand|Select|Wrap|Line Numbers
  1. if (Amanda == 0)
  2.      cout<<"She does"<<endl;
  3. else
  4.      cout<<"She doesn't"<<endl;
  5.  
Is it just all about how you decide to implement it?
Nov 14 '07 #5
Laharl
849 Recognized Expert Contributor
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,
Expand|Select|Wrap|Line Numbers
  1. enum boolean {FALSE, TRUE};
  2.  
  3. SomeStructure::boolean isEmpty(){
  4.   if (size == 0)
  5.       return TRUE;
  6.   return FALSE;
  7. }
  8.  
is equivalent to:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SomeStructure::int isEmpty(){
  3.    if (size == 0)
  4.      return 1;
  5.    return 0;
  6. }
  7.  
Nov 14 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.

Expand|Select|Wrap|Line Numbers
  1. enum Status { yes, no};
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. if (Amanda == yes)
  2.      cout<<"She does"<<endl;
  3. else
  4.      cout<<"She doesn't"<<endl;
  5.  
Rather than:

Expand|Select|Wrap|Line Numbers
  1. if (Amanda == 0)
  2.      cout<<"She does"<<endl;
  3. else
  4.      cout<<"She doesn't"<<endl;
  5.  
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.

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.
Nov 15 '07 #7
Laharl
849 Recognized Expert Contributor
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.
Nov 15 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
Keep posting. You probably know a lot more that you are letting on.
Nov 16 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4181
by: Ray Gardener | last post by:
I was wondering if anyone had tried implementing fuzzy logic set concepts in C++, because in FL, the concept of "type" or "class" is fuzzy; things belong (or are of) a given type only by degree....
9
3116
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
9
6188
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
24
14378
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.