Practicing vs. Learning and switches inside of switches

Started by
3 comments, last by NightCreature83 12 years, 2 months ago
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");
}
}
}
Advertisement
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;
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.

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;
}

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement