-
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.
-
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.
-
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
-
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.
-
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";
}
-
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;