Public Group

# Practicing vs. Learning and switches inside of switches

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

## Recommended Posts

I've been posting a lot since I joined this week trying to make a simple text game. I've only been doing C++ a week and a few days, but, even though I feel like I learned a fair amount in that time, maybe instead of trying to create, I should keep learning so I'm better equipped to make what I'm aiming at. I wanted to make the game only to solidify the code I have learned. I'm afraid maybe I am setting bad habits in stone, instead.

In this game, I was successful in making and learning how to make classes. I put in a few basic functions in source files. Today, I wanted to add in a menu function the game could be played and adjusted within. The problem is the string's seem to never break. Here is what I did, which is the result of me rethinking how I should be spending my time with C++.

within the main.cpp
 while(ourHero.getHealth()>0) // Our main player(class) { cout << "Please make a selection:\n\n[1]The Armory\n\n[2]The Pit\n\n[3]Item Shop\n\n[4]Quit Game\n"; int menuSelect; cin >> menuSelect; switch(menuSelect) { case 1: { system("cls"); cout << "Welcome to The Frorth City Armory\n\n"; Sleep(1500); cout << "What would you like to do?\n\n[1]Upgrade Main-hand Weapon\n\n[2]Upgrade Off-hand Weapon\n\n[3]Exit\n"; int choice; cin >> choice; switch(choice) { case 1: { ourWeapon.sharpenWeapon1(); system("cls"); cout << "Main-hand sharpened!"; break; } case 2: { ourWeapon.sharpenWeapon2(); system("cls"); cout << "Off-hand sharpened!"; break; } case 3: { break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue\n"; cin.ignore(); cin.get(); system("cls"); } } break; } case 2: { cout << " The Pit is currently closed!"; } case 3: { cout << "Out to lunch"; break; } case 4: { cout << "Are you sure you wish to exit Sabra?\n[Y]es?"; cin.get(); break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue\n"; cin.ignore(); cin.get(); system("cls"); } } }

##### Share on other sites
hi,

while (hero.health > 0)
-> this will make your loop neverending story ;) nowhere inside switch statement si hero's health lowered so the statement allways evaluates to true

'the pit case' is missing break

'exit' case -> you need to evaluate players input wheter it was Y or not instead of checking hero's health make bool variable and set it accordingly to correctly evaluate state of the game:

bool running = true;
while(running)
{
switch code here

}

in exit case simply set running to false;

##### Share on other sites
It's odd I overlooked not placing some cin.ingore()/cin.get()s here and there, plus missing those breaks. I'm panicking over the smallest things. This is mostly fixed, minus the functions that obviously need added. My only problem is figuring out how to make the "Exit the game" function work.

##### Share on other sites

It's odd I overlooked not placing some cin.ingore()/cin.get()s here and there, plus missing those breaks. I'm panicking over the smallest things. This is mostly fixed, minus the functions that obviously need added. My only problem is figuring out how to make the "Exit the game" function work.

One solution would be to:
#1, change your while condition to check for bRunning bool.
#2, create a function to check for quit confirmation, and true a bool

Something like this:
 bool bRunning = true; while(bRunning) { // present menu, read in players option, where 'q' is the quit option switch (option) { ... case 'q': if (ConfirmQuit()) { bRunning = false; } break; } } cout << "Thanks for playing!\n"; bool ConfirmQuit() { char quit; cout << "Are you sure you want to quit? (type y to confirm)\n"; cin >> quit; if (quit == 'y') { return true; } return false; } 

##### Share on other sites
A switch within a switch is generally fine but in most cases you will see a function call in its place and the second switch is then in that function. This keeps your first switch shorter and easier to read.

Also I personnaly don't like prefixing variables with "my" or "our" they imply that there can be only one of these which isn't always the case, and if it was I would use "the" as the prefix. You should see variable declarations as nouns in a normal sentence and functions and method calls as verbs.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633705
• Total Posts
3013463
×