C++ enumeration

Started by
5 comments, last by Zahlman 18 years, 9 months ago
Hey, Im currently trying to learn C++ with the help of 2 books, 1 being 'begining C++ game programming' and it sets you 1 or 2 task at the end of each chapter. Im at the end of chapter 2 and its set a task of rewriting a menu chooser program contained in chapter 1 using enumeration to represent difficulty level's, the problem is both book's I have only touch up on enumeration and uses it like so:-

#include <iostream>
using namespace std;

int main()
{
  const int ALIEN_POINTS = 150;
  int aliensKilled = 10;
  int score = aliensKilled * ALIEN_POINTS;
  cout << "Score: " << score << endl;

  enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};
  difficulty myDifficulty = EASY;

  enum ship {FIGHTER = 25, BOMBER, CRUISER = 50, DESTROYER = 100};
  ship myShip = BOMBER;
  cout << "\nTo Upgrade my ship to a cruiser will cost: " << (CRUISER - myShip) << " Resource Points.\n";

  cin.ignore(cin.rdbuf()-> in_avail() + 1);
  return 0;
}

So how do i turn the enumeration into a selectable menu and not one thats already predefined, I dont really want the code as an answer just pointing in the right direction please. Thanks for any help.
Advertisement
Well I don't know about the menu not being predifined; that's kind of the point of enumerations, right?

But for the menu, you would display something like:
Please select a difficulty level:1) Easy2) Medium3) Hard


So then the user inputs a number corisponding with the difficulty they want. So say the user input 1. You would then compare that number with your different enumerations and find that Easy is == 1 (assuming you added that to you enum).

If this seems what you are looking for but don't quite understand let me know and I'll put up some code.

Hope that helps,
Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Since you have to rewrite the program from the first chapter, those who don't have the book probably won't be much help unless you can give the source for the program you have to rewrite.
Yeah it is that sort of menu and sorry for confusing you about the menu bein predefined, it was just in the above code is was set at one level and not selectable.
Anyway yeah what i want to know is how do you take the user's input and compare it to the enumeration's i have set for the menu.

Thanks.

Below is the code of the menu I have to rewrite.
#include <iostream>using namespace std;int main(){  cout << "Difficulty Levels>\n\n";  cout << "1 - Easy\n";  cout << "2 - Normal\n";  cout << "3 - Hard\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;}


[Edited by - dek001 on July 9, 2005 5:04:33 PM]
He's a basic menu set up. You should able to figure things out from here:

// Needed for cin and cout#include <iostream.h>// Our enumerationsenum { DX = 1, OGL, ANY, NUM_OPTIONS };// Entry point for the programint main(int argc, char* argv[]){	// Display our menu	cout	<< "Please select an option:\n"			<< "1) Direct3D\n"			<< "2) OpenGL\n"			<< "3) Doesn't matter. They're both good and anyone who argues different is a moron\n\n"			<< "Selection: ";	// This will store our selection	int selection = -1;	// Get the user's choice	cin >> selection;		// Now check what they selected	switch(selection)	{		case DX:			{				// They selected Direct3D				cout << "\nDirect3D it is!\n\n";				break;			}		case OGL:			{				// They selected OpenGL				cout << "\nOpenGL it is!\n\n";				break;			}		case ANY:			{				// They decided each is cool in its own way				cout << "\nIndifference it is!\n\n";				break;			}	}	return 0;}


Update: So yeah it looks from you last post is that all you're missing is that the enum basically become the number you enumerate them to. For example, in my example DX is enumed to the number 1. Therefore I can use DX as if it were a regular constant (ie. 1, -4, 3986, 69.01).

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Yep that helped alot got it sorted now, wasnt sure on enumeration's as the book only use's them once or twice and not for a menu type system it was asking for.

Thanks very much for your help.
Note: an enumeration does not "know" the enumeration names at run-time; they are for the programmer's convenience. To simplify things (so that you have all your data in one place, and to avoid repeating the names throughout the code) you should consider making a table of the names that match the enumerations as well:

enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};// I can use string literals here because these are constantsconst char* difficultyNames[] = {"Novice", "Easy", "Normal", "Hard", "Unbeatable"};// This works because we have an array of pointers, not a pointer to the pointers.// It will keep us from having to keep the count in sync ourselves if we change// the number of difficulty levels.const int difficultyCount = sizeof(difficultyNames) / sizeof(const char*);// Outputting the menu, for example:for (int i = 0; i < difficultyCount; i++) { // for each item  // We want to output (the item number + 1) first, as a label (i.e. start counting at 1)  // and then a close paren, and then the name.  std::cout << (i+1) << ") " << difficultyNames << endl;}

This topic is closed to new replies.

Advertisement