Public Group

# Beginning c++ through game programming third edition, solutions to exercise advise.

This topic is 2168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, Good afternoon.
I've been working on this book for the past 2 days, and stumbled upon an exercise. i would like to get feedback as to whether my solution is good, or if i could have made it much simpler. any tips would be greatly appreciated.

This is the original Menu Chooser.cpp.

 // Menu Chooser // Demonstrates the switch statement #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; } 

These are the instructions.

Exercises
1. Rewrite the Menu Chooser program from this chapter using an enumeration
to represent difficulty levels. The variable choice will still be of type
int.

And this is my rewritten version of the Menu Chooser.cpp

 // Menu Schooser // Demonstrates the switch statement #include<iostream> using namespace std;int main() { enum difficulty { EASY = 1, NORMAL, HARD }; difficulty Easy = EASY; difficulty Normal = NORMAL; difficulty Hard = HARD; bool success = true; do { cout << "Please select Difficulty Level.\n\n"; cout << "1 - Easy\n"; cout << "2 - Normal\n"; cout << "3 - Hard\n\n"; int choice; cout << "Choice: "; cin >> choice; if ( choice == Easy ) { cout << "\nYour difficulty is set to Easy.\n"; success = true; } else if ( choice == Normal ) { cout << "\nYour difficulty is set to Normal.\n"; success = true; } else if ( choice == Hard ) { cout << "\nYour difficulty is set to Hard.\n"; success = true; } else { cout << "\nInvalid selection!!\n\n"; success = false; } } while(!success); return 0; } 

Is this re-written code viable or is there a simpler way i can re write the code.

Thanks in advance for the feedback.

##### Share on other sites
There is no point in creating a variable for every single enum value (and get into the habit of declaring them const, when they aren't supposed to change)

The location of declaring choice is plain weird, declare them right before they are used for the first time and in their own line (and initialize... always). What if you fail to read because the user entered "slkjsfg"? Choice will still have some random value.

Don't use if/else chains when a switch is perfectly fine.

Put success _inside_ the loop, inizialize to true and avoid setting it in every single if block (removing the need to do something in a million places removes the possibility to forget it half the time).

##### Share on other sites
Hi all,

Im having problems with the same exercise. At the risk of jumping to conclusions, i will say that it seems to be a trick question.(maybe because i dont know enough yet?) The reason i say that is because in the chapter about enums we learn that they are constants. Yet, the program we are supposed to make is supposed to allow the user to input what difficulty they want, correct? So the question i have is how is the user supposed to change a constant? Isnt that illegal/impossible? Or has the past 2 chapters just flown right over my head? Am i missing something?

##### Share on other sites
The values in anenumdeclaration are constant. So EASY in rhe above code will always have a value of one. But a variable of type difficulty can be assigned a value of EASY, NORMAL or HARD; andcan be reassigned a value as needed.

I'd also like to note that examples tend to use "using namespace std" alot. This is done for convenience and is not in fact good coding practice. It really is better to get in the habit of typing std:: than bringing everything in std into the global namespace.

• 10
• 18
• 14
• 18
• 15