   Need help with C++ exercise
  Hey all, first post here :)I'm working my way through Beginning C++ Through game programming by Michael Dawson.And I'm stuck on an exercise, and need you expertise ;)The exercise: Rewrite the Menu Chooser program from this chapter using an enumeration to represent difficulty levels. The variable choice will still be of type int.The program:// Menu Chooser// Demonstrates the switch statement#include <iostream>using std::cout;using std::cin;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;}My version using enumerators: (Not working)// Menu Chooser 2.0 (ENUM)#include <iostream>using std::cout;using std::cin;int main(){cout << "Difficulty Levels\n\n";cout << "1 - Easy\n";cout << "2 - Normal\n";cout << "3 - Hard\n";enum difficulty {Easy = 1, Normal, Hard};difficulty 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;}Apparently you can't to cin on an enumerator so I'm out of ideas. I don't know how to solve this problem. That's why I need you help.Thanks in advance!Please don't mark threads "solved" on this forum. -- jpetrie[Edited by - jpetrie on July 1, 2009 10:18:20 AM]
int i;cin >> i;choice = static_cast<difficulty>( i );or:int i;cin >> i;switch ( i ){case 1:choice = Easy; break;case 2: choice = Normal; break;case 3: choice = Hard; break;default: cout << "Invalid selection\n";};  Thanks! It worked.Just one more question, can you explain this line:choice = static_cast<difficulty>( i );;  It converts i from its type, int, to difficulty; see static_cast and A comparison of the C++ casting operators.   ;  I see.Thanks again.   ;  Using the enumeration, you will not be able to read in directly to a variable of that type (unless you implement it yourself; but you can implement it to do neater things, e.g. interpret the text "easy" as the enumeration value easy), but it will let you use the enumeration values for cases in the switch statement (and you should do so).   