Thread: enumeration

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    enumeration

    Hi,

    I'm having two problems with my code. One is implenting enumeration into it....And the other (which is frustrating because it's USUALLY easy to do) is getting it to exit when the user does not enter anything.

    To better describe my enumeration problem....I have to implement the line that is currently in comment

    //enum strfunct {strcat, strchr, strcpy, strcmp, strlen, strstr, strtok};

    Right now it compiles and runs fine (Except for the exiting thing), but I have to use enumeration for the string functions. Any help would be appreciated.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    enum boolean {FALSE = 0, TRUE = 1};
    //enum strfunct {strcat, strchr, strcpy, strcmp, strlen, strstr, strtok};
    int main(void)
    {
    int i;
    enum boolean Found;
    enum strfunct fct;
    char string[50];
    char *str_names[] = {
    "strcat",
    "strchr",
    "strcmp",
    "strcpy",
    "strlen",
    "strstr",
    "strtok"
    };

    //get user string and compare it with string names
    printf( "Enter a possible C string function: " );
    gets (string);
    do

    {
    Found = FALSE;

    //compare and see if they're equals
    for (i=0; i<7; i++)
    {

    if (strcmp(string,str_names[i]) == 0)
    {
    Found = TRUE;
    }

    else if (strcmp(string,str_names[i]) == 1)
    {
    Found = FALSE;
    }
    }

    if (Found == TRUE)
    {
    printf("%s is a C defined string function\n",string);
    }
    else if (Found == FALSE)
    {
    printf("%s is NOT a C defined String function\n", string);
    }
    printf( "\nNext function, please: " );
    gets (string);

    }
    while (string != NULL);

    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Edit your post and add code tags please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    comments

    I hope I've commented this well enough to get a better idea of what I'm doing and trying to do.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //enum to set FALSE to 0 and TRUE to 1
    enum boolean {FALSE = 0, TRUE = 1};
    
    /*This next line is my area of confusion....I know it's not common to use it
    this way, but I have to use it in the program to find if the string entered 
    is found in this enum list.*/
    //enum strfunct {strcat, strchr, strcpy, strcmp, strlen, strstr, strtok};
    int main(void)
    {
        int i;
    	enum boolean Found;
    	enum strfunct fct;
    	char string[50];
    
    	//Pointer array of String names
    	char *str_names[] = {
            "strcat",
            "strchr",
    		"strcmp",
    		"strcpy",
    		"strlen",
    		"strstr",
    		"strtok"
        };
        
    	//get user string and compare it with string names
        printf( "Enter a possible C string function: " );
    		gets (string); 
    	
    	do
    
    	{
    		//Initialize Found to False
    		Found = FALSE;
       
    		//compare and see if they're equals starting a the first element in the array
    		for (i=0; i<7; i++)
    		{
    			/*if statement to compare the string entered to each string name and 
    			if it's Found it sets Found to TRUE*/
    			if (strcmp(string,str_names[i]) == 0)
    			{
    				//Sets Found to TRUE (1)
    				Found = TRUE;
    			}
    			/*else if statement to compare the string entered to each string name and 
    			if it's NOT Found it sets Found to FALSE*/		
    			else if (strcmp(string,str_names[i]) == 1)
    			{
    				//Sets Found to FALSE(0)
    				Found = FALSE;	     
    			}    
    		}
    		
    		//Display if the string name entered was an actual C String function
    		if (Found == TRUE)
    		{
    			printf("%s is a C defined string function\n",string);
    		}
    		//Display if the string name entered was NOT an actual C String function
    		else if (Found == FALSE)
    		{
    			printf("%s is NOT a C defined String function\n", string);
    		}	
    		//Prompts user to enter another string and gets it
    		printf( "\nNext function, please: " );	
    		gets (string);
    		
    	}
    	//Exits if no string is entered
    	while (string != NULL);
    	
        return 0;
    }
    Code tags added by Hammer

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let's say you've got this.
    Code:
    #include <string.h>
    enum strfunct {strcat, strchr, strcpy, strcmp, strlen, strstr, strtok};
    Which strcat is a function and which strcat is an enumerated integer constant? The compiler can't tell either.


    Use different "names" for the enumeration if you're going to use them, maybe something like this.
    Code:
    enum estrfunct { CAT, CHR, CPY, CMP, LEN, STR, TOK };
    But you're not using them anyway (the variable fct is not used).


    Don't use gets. There are alternatives. Also, try not to use reserved identifiers. And again, investigate [code] [/code] tags.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I know my reply was only one line long, but please don't tell me you missed it??
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Appologies

    My appologies....I misunderstood. I do see what you mean and will post with code tags from now on...I won't bother reposting this one, but from now on will do it that way. Sorry about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding/Implementing Enumeration
    By Iconate in forum C Programming
    Replies: 15
    Last Post: 10-10-2008, 09:16 AM
  2. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  3. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  4. Having trouble implementing enumeration.
    By RP319 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:03 PM
  5. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 08:59 AM