-
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;
}
-
Edit your post and add code tags please.
-
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
-
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.
-
I know my reply was only one line long, but please don't tell me you missed it?? :confused:
-
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.