2

I have what I'm sure is a simple problem, but I can't figure it out. In the code below, I'd like to be able to have case 5 re-display the options afterward. How can I do this? Thanks in advance!

// Input the race of your character
    cout << "Choose a race here: " << endl
    << "1) Human, 2) Elf, 3) Dark Dwarf, 4) Commoner, 5) Race info, 6) Admin Debug Race : ";

    cin >> mCharRace;

    switch (mCharRace)
        {
        case 1:
        cout << "You have chosen Human!" << endl;
        mExpPoints = 999;
        mArmor = mArmor + 2;
        break;
    case 2:
        cout << "You have chosen Elf!" << endl;
        mAccuracy = mAccuracy + 2;
        mWeapon.mDamageRange.mLow = mWeapon.mDamageRange.mLow + 1;
        break;
    case 3:
        cout << "You have chosen Dark Dwarf!" << endl;
        mWeapon.mDamageRange.mHigh = mWeapon.mDamageRange.mHigh + 2;
        mMaxHitPoints = mMaxHitPoints + 3;
        break;
    case 4:
        cout << "You have chosen Commoner! Brave man." << endl;
        mAccuracy       = mAccuracy - 3;
        mHitPoints      = mHitPoints - 5;
        mMaxHitPoints   = 8;
        mExpPoints      = -250;
        mNextLevelExp   = 1500;
        mArmor          = -1;
        break;
    case 5:
        cout << "Placeholder for explanation text." << endl;
        break;
    case 6:
        cout << "ADMIN POWERS UNITE!!!!!!!" << endl;
        mAccuracy       = 20;
        mHitPoints      = 1000;
        mMaxHitPoints   = 1000;
        mExpPoints      = 0;
        mNextLevelExp   = 1000;
        mArmor          = 100;
        mWeapon.mName   = "Admin Sword of HNNNG!";
        mWeapon.mDamageRange.mLow   = 100;
        mWeapon.mDamageRange.mHigh  = 150;
        mGold           = 1000000;
        break;
Vladimir Marenus
  • 393
  • 2
  • 3
  • 16
  • 1
    Not an answer, 'cause I don't want to get hammered. There's always Duff's device: http://en.wikipedia.org/wiki/Duff's_device . – David Hammen Oct 26 '12 at 15:27

4 Answers4

12

A switch-case construct isn't an iteration construct. It can only select a certain case and execute it. The flow of control cannot go back up due to it.

So, you can't use it to loop. Wrap it in a while or for looping construct instead.

while(condition){
   switch (mCharRace){
   ...
   ...
   }
}

Just turn condition to false when you want to stop looping.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
6

Wrap the input and switch code in a loop, and assign a variable exitLoop to exit the loop. Set that variable to TRUE by default, except in those cases where you want to repeat the input. In those, set exitLoop to false.

LSerni
  • 55,617
  • 10
  • 65
  • 107
1

Add a label before your first cout

label:

cout << "Choose a race here: " << endl
<< "1) Human, 2) Elf, 3) Dark Dwarf, 4) Commoner, 5) Race info, 6) Admin Debug Race : ";

cin >> mCharRace;
...

then inside the case 5, add

goto label;
cjamesm
  • 109
  • 7
1

Put it inside a for(;;) loop and use continue instead of break. Put another break after the end of the switch statement to catch the cases that really do want to break:

for (;;)
{
    switch (x)
    {
    case case_that_wants_to_loop:
        // ...
        continue;
    case case_that_wants_to_break:
        // ...
        break;
    }
    break;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • @downvoter This technique is used in commercial interpreters. It is considerably more efficient than the alternatives, especially the ones that use a boolean variable. – user207421 Jun 17 '21 at 00:45