Searching an array for enum type

I have an array that has types char, char, int, and enum type Category.
I have to search the array for the entries matching a certain Category. How would I do this? I can do a linear search for finding entries based on the other parts of the array, but how would i search for one in the enum type?
Last edited on
You would want to enhance FindTitle to accept a Category as an argument and then serach to entries that match both the string and category.

Would have been helpful if you had posted what you have for FindTitle.
what I have for FindTitle function is as follows.

Would I have to make a separate function to search for the Categories or is there indeed a way to use this function for the Categories too?

1
2
3
4
5
6
7
8
9
10
11
12
13

int List::FindTitle(char* anEntry) const
{
for (int i = 0; i < currentSize; i++)
	{
		if ( strcmp(entryList[i].GetTitle(), anEntry) == 0)
			return i;
		else if (strcmp(entryList[i].GetAuthort(), anEntry) ==0)
			return i;
	}

	return -1;
}
Last edited on
Again, you really have not shown enough information. What does entryList look like? What does youe enum look like? Is your enum a member of entryList?

If your entries in the list are generic, i.e. You have one text field distinguished by the enum, then I would implement something like the following:

1
2
3
4
5
6
7
8
int List::Search (char* anEntry, enum EntryType et) const
{ for (int i = 0; i < currentSize; i++)
  { if ( strcmp(entryList[i].GetText(), anEntry) == 0
          && entryList[i].get_type() == et)
	return i;
  }
  return -1;
}


If you entries are not generic (i.e. separate text fields in entryList for each item, then I would implement something like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
int List::Search (char * anEntry, enum EntryType et) const
{ for (int i = 0; i < currentSize; i++)
  { switch (et) 
    {
    case et_title: if (strcmp(entryList[i].GetTitle(), anEntry) == 0)
	            return i;
    case et_auth: if (ctrcmp(entrylist[i].GetAuthort(), anEntry) == 0)
                             return i;
    // etc
    }
  }
  return -1;
}


Last edited on
my enum types are

1
2
3

enum Category {SONG, POEM, ESSAY, NOVEL, NOTE};


Each entryList entry has a title(char array), an author (char array), type (Category), and size (int).

I already have a function that searches the entryList for a matching title, but this function I am asking about has to print the titles of all the entries in a certain category. This is where I am stuck.

print the titles of all the entries in a certain category


How about this?
1
2
3
4
5
6
7
8
9
10
int List::PrintByCategory (Category cat) const
{   int cnt = 0;  // Number of matching titles
    for (int i = 0; i < currentSize; i++)
    {  if (entrylist[i].category == cat)
        {  cout << entrylist[i].title << endl;
            cnt++;
        }
  }
  return cnt;
}  

nevermind, I used accessor functions instead of .category and .title and as of now there are nto any errors
Last edited on
Topic archived. No new replies allowed.