Sunday, August 2, 2009

How can I write a C++ program using an enumeration and the switch statement for more than one value?

int main()


{


cout %26lt;%26lt; "Difficulty \n\n"


cout %26lt;%26lt; "1- Easy"


cout %26lt;%26lt; "2-Normal"


cout %26lt;%26lt; "3-Hard"





int choice;


cout %26lt;%26lt; "Choice:"


cin %26gt;%26gt; choice;


switch (choice);


{


case 1:


cout %26lt;%26lt; "You pick Easy";


break;


case 2:


cout %26lt;%26lt; "You picked Normal";


break;


case 3:


cout %26lt;%26lt; "You picked Hard";


break;


default:


cout %26lt;%26lt; "You made an illegal choice."


}


return 0;


}





How to rewrite that using an enumeration to represent difficulty levels.?

How can I write a C++ program using an enumeration and the switch statement for more than one value?
int main()


{


enum difficulty {


zero,


Easy,


Normal,


Hard,


};


difficulty choice;


cout %26lt;%26lt; "Difficulty \n\n"


cout %26lt;%26lt; "1- Easy"


cout %26lt;%26lt; "2-Normal"


cout %26lt;%26lt; "3-Hard"





cout %26lt;%26lt; "Choice:"


cin %26gt;%26gt; choice;


switch (choice);


{


case Easy:


cout %26lt;%26lt; "You pick Easy";


break;


case Normal:


cout %26lt;%26lt; "You picked Normal";


break;


case Hard:


cout %26lt;%26lt; "You picked Hard";


break;


default:


cout %26lt;%26lt; "You made an illegal choice."


}


return 0;


}

elephant ear

No comments:

Post a Comment