Reputation: 403
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;
Upvotes: 3
Views: 48454
Reputation: 310980
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;
}
Upvotes: 1
Reputation: 109
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;
Upvotes: 1
Reputation: 46768
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.
Upvotes: 13
Reputation: 57418
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.
Upvotes: 6