Thread: Enumeration

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Enumeration

    I am having a real hard time manipulating enumerations. What I am trying to do is make a guessing game that has a state's name in the enumeration block. The user types in a state, and the program needs to tell them if they guessed the right one. Each state is represented as a number since it's enumerated and I have the program randomly guessing numbers. I am lost on how to manipulate the enumerated data in collaberating with the random numbers that the program is using.

    I hope my explanation wasn't confusing.

    Any ideas would be appreciated. Thank you.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Something like so?

    Code:
    enum options { HOUSE, DOG, CAT, FRED, NUMOPTIONS };
    
    char c_options[NUMOPTIONS][256]=
    {
        "House",
        "Dog",
        "Cat",
        "Fred",
    };
    Code:
    cout << c_options[HOUSE] << endl;
    Will output House.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Or something slightly more robust when dealing with invalid "states".
    Code:
    #include <iostream>
    using namespace std;
    
    enum State_t
    {
        STATE_1,
        STATE_2,
        STATE_3,
        STATE_4,
        STATE_5,
        STATE_6,
        STATE_7,
    };//State_t
    
    struct StateNamePair
    {
        State_t m_state;
        const char *m_name;
    };//StateNamePair
    
    const StateNamePair StateNameTbl[] = 
    {
        {STATE_1, "STATE_1"},
        {STATE_2, "STATE_2"},
        {STATE_3, "STATE_3"},
        {STATE_4, "STATE_4"},
        {STATE_5, "STATE_5"},
        {STATE_6, "STATE_6"},
        {STATE_7, "STATE_7"},
    };//StateNameTbl
    
    const char* LookupStateName(int state)
    {
        const int StateNameTbl_SZ = sizeof(StateNameTbl) / sizeof(*StateNameTbl);
        
        for (int n = 0; n < StateNameTbl_SZ; n++)
        {
            if (state == StateNameTbl[n].m_state)
                return StateNameTbl[n].m_name;
        }//for
    
        return 0;
    }//LookupStateName
    
    int main()
    {
        const char *state_name = LookupStateName(STATE_1);
    
        if (state_name)
            cout << "Found name: " << state_name << endl;
        else
            cout << "Name not found." << endl;
    
        return 0;
    }//main
    gg

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Well, if the states are invalid, I will just count them as a wrong guess.
    The logic for my program is kind of like this: User types in a state. If state is equal to the program-generated number, display message, "You've got it. It took you this # of times." If state is not equal to program generated number, try again, and increment number of guesses by one.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    This is what I have so far. I think I'm in the ballpark, but not fully there yet. Any ideas would be great.

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    void guess(string& input, int& computer_number);
    enum block {florida, wisconsin, california};
    block states;
    
    int main() {
        srand(time(0));
        int computer_number = rand()%6;
    	string input;
    
    	cout << "Enter the state: ";
    	cin >> input;
    
    	guess(input, computer_number);
    
    	return 0;
    }
    
    void guess(string& input, int& computer_number) {
    	switch (input) {
    	case florida: colors = florida;
    		break;
    	case wisconsin: colors = wisconsin;
    		break;
    	case california: colors = california;
    		break;
    	default: cout << "Invalid input";
    	}

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    string input;
    ...
    ...
    switch (input)
    In a switch statement, input has to be an integer. A series of if-elseif statements does the same thing as a switch statement, so in this case use if-elseif's to compare the input to the various states.

    You haven't declared a variable called colors:
    Code:
    void guess(string& input, int& computer_number) {
    	switch (input) {
    	case florida: colors = florida;
    Last edited by 7stud; 04-10-2005 at 03:15 PM.

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
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM