Skip to main content
gamedev.net
🔒 Locked

switch to enumaration?

Started by Lonefox1 Feb 22, 2005 at 3:59 PM 5 replies 900+ views
Original Post
Lonefox1
Lonefox1
Hi guys wasnt sure whether to put this in game programming or not but seen as its only basic console stuff i chose to put it in here. sorry if its in wrong place! im currently reading the book "beginning C++ Game Programming" and at the end of chapter 2 it asks me to rewrite a program using enumarators, but im unsure what it wants me to do :| heres the original code:

#include <iostream>
using namespace std;


int main() 
{
	cout << "Difficulty Levels\n\n";
	cout << "1 - Easy\n";
	cout << "2 - Normal\n";
	cout << "3 - Hard\n\n";

	int choice;
	cout << "Choice: ";
	cin >> choice;

	switch (choice)
	{
	case 1:	
			cout << "You picked Easy.\n";
			break;
	case 2:	
			cout << "You picked Normal.\n";
			break;
	case 3:	
			cout << "You picked Hard.\n";
			break;
	default:
			cout << "You made an illegal choice.\n";
	}

    return 0;
}


now im assuming it wants me to use enumarators as my choice for the switch cases so im trying to do the following but it wont compile properly so im unsure as to what it wants me to do. the question is: "Rewrite the menu chooser program from this chapter using an enumeration to represent difficulty levels." heres the code i have:

#include <iostream>
using namespace std;


int main() 
{
	cout << "Difficulty Levels\n\n";
	cout << "1 - Easy\n";
	cout << "2 - Normal\n";
	cout << "3 - Hard\n\n";
    
    
    enum difficulty { EASY = 1, NORMAL , HARD};
    int mydiff;
    
    cout << "enter a number 1 to 3 for difficulty level: " << endl;
    cin >> mydiff;
    
    difficulty selection = mydiff;
	
	

	switch (selection)
	{
	case 1:	
			cout << "You picked Easy.\n";
			break;
	case 2:	
			cout << "You picked Normal.\n";
			break;
	case 3:	
			cout << "You picked Hard.\n";
			break;
	default:
			cout << "You made an illegal choice.\n";
	}

    return 0;
}


as you can see im trying to get one of the enum numbers into the switch case, but ive no idea if this is what it wants or how to impliment my idea :( any ideas? if anyone knows where the exercise answers are to this book could they paste a link too? ive had a search around and cant find anything. any help appreciated lonefox
Evil Steve
Evil Steve
You've almost got it. Here (untested):
enum difficulty { EASY = 1, NORMAL , HARD}; // Needs to be outside a functionint main() {    cout << "Difficulty Levels\n\n";    cout << "1 - Easy\n";    cout << "2 - Normal\n";    cout << "3 - Hard\n\n";    int mydiff;    cout << "enter a number 1 to 3 for difficulty level: " << endl;    cin >> mydiff;    difficulty selection = mydiff;	    switch (selection)    {    case EASY:	        cout << "You picked Easy.\n";        break;    case NORMAL:	        cout << "You picked Normal.\n";        break;    case HARD:	        cout << "You picked Hard.\n";        break;    default:        cout << "You made an illegal choice.\n";    }    return 0;}

You just need to change the numbers in the enumeration to the values.
Billr17
Billr17
You need to typecast the int "mydiff" to the enum type of "difficulty" as shown below. Then instead of the case statements using integer values, use the enum values you have created (EASY, NORMAL, HARD). This should fix you problems. BTW next time you have a basic question on C++ it should be posted in the "For Beginners" Forum.

#include <iostream>using namespace std;int main() {	cout << "Difficulty Levels\n\n";	cout << "1 - Easy\n";	cout << "2 - Normal\n";	cout << "3 - Hard\n\n";            enum difficulty { EASY = 1, NORMAL , HARD};    int mydiff;        cout << "enter a number 1 to 3 for difficulty level: " << endl;    cin >> mydiff;        // Typecast the int to the difficulty type.     difficulty selection = (difficulty) mydiff;		        // Use the keywords instead of the actual integer values.	switch (selection)	{	case EASY:				cout << "You picked Easy.\n";			break;	case NORMAL:				cout << "You picked Normal.\n";			break;	case HARD:				cout << "You picked Hard.\n";			break;	default:			cout << "You made an illegal choice.\n";	}    return 0;}
Lonefox1
Lonefox1
thanks guys, not sure im on the right track tho, the book hasnt really touched on typecasting in the first 2 chapters only briefy but not mentioning typecasting an enumeration O_O ill save this code and come back to it i think :D thanks for the help tho. and ill head over the beginner forums from now on.

cheers
lone
bkt
bkt
int main(void) {  enum diff_types_e {    TYPE_EASY=1,TYPE_NORMAL,TYPE_HARD  };  cout << "Difficulty Levels\n\n";	cout << TYPE_EASY << " - Easy\n";	cout << TYPE_NORMAL << " - Normal\n";	cout << TYPE_HARD << "- Hard\n\n";        int iChoice;        cin >> iChoice;                  switch(iChoice) {           case TYPE_EASY:           case TYPE_NORMAL:           ...

-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Zahlman
Zahlman
Quote:
Original post by Lonefox1
thanks guys, not sure im on the right track tho, the book hasnt really touched on typecasting in the first 2 chapters only briefy but not mentioning typecasting an enumeration O_O


Enjoy. [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.