Switch case error.

Hey guys, I'm creating an RPG for my C++ class and I've run into a bit of an awkward error with a switch case. Here is my main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Player.h"
#include "Enemy.h"
#include "Battle.h"
#include "Equipment.h"
#include "Shop.h"

using namespace std;

int main() {

	short unsigned int testChoice;
	enum TestChoices { QUIT, SHOP, BATTLE };
	Player* newPlayer = new Player("Jerk", 1, 1, 0, 1, 0, 10, 0, 10, 6000, 10);
	Shop* newShop = new Shop(newPlayer);
	srand(static_cast<unsigned int>(time(0)));

	do {

		cout << "Welcome to the dummy test program." << endl << endl;
		cout << "The following options are available:" << endl << endl;
		cout << "1 - Test Store" << endl;
		cout << "2 - Test Battle" << endl;
		cout << "0 - Quit" << endl << endl;
		cout << "Pick one: ";
		cin >> testChoice;

		switch(testChoice) {

			case QUIT:

				cout << endl << "Fine, peace!" << endl << endl;
				break;

			case SHOP:
				
				newShop->BeginShopping();
				break;
				
			case BATTLE:

				Enemy* newEnemy = new Enemy("Guy", 10, 9000, 1, 9000, 5, 1, 2);
				Battle* newBattle = new Battle(newPlayer, newEnemy);
				newBattle->BeginBattle();
				delete newEnemy;
				delete newBattle;
				break;
			
		}

		ShowPlayer(*newPlayer);

	} while (testChoice != QUIT);

	return 0;

}


This code actually works properly. The odd part is if I don't include scope modifiers in case SHOP, and then move Shop* newShop = new Shop(newPlayer); into case SHOP, the compiler (Visual Studio Express 2010) gives me an error that the initialization of new shop was skipped by the case label. I find it odd that the same thing doesn't happen for case BATTLE, but if I move the initializations of battle and enemy to case SHOP, it does give me the same error.

Even if I add delete newShop immediately after BeginShopping ends, it still won't accept it unless I use scope modifiers. Thoughts?

EDIT: My explanation probably doesn't make sense, so here is the code that gives me an error:

1
2
3
4
5
6
case SHOP:

				Shop* newShop = new Shop(newPlayer);
				newShop->BeginShopping();
				delete newShop;
				break;
Last edited on
It's because if you create an object in the SHOP case, then exit the switch only line 48, the destructor will destroy it. However, if you entered the BATTLE case, the destructor would still try to be called. Hence the requirement for the scope there. IMO, you should put them in BATTLE too.

Btw, why are you allocating all this stuff on the heap? You aren't using any of the benefits provided by the heap, so just use the stack; you won't have to deal with new/delete here.
To allocating them in heap: A requirement is using the heap to allocate data. :) This felt the most appropriate.

To using everything in BATTLE: Why is that? The program asks the user what they want to do, so if they type in 1, it will go to the shop, if they type in 2, a battle will start, etc.

EDIT: AHH.. I see what you're saying about putting them in battle. You meant the scope modifiers, and I 100% agree with you.
Last edited on
I'm not sure if this is your problem but when declaring variables inside switch cases, you need curly braces
1
2
3
4
5
6
7
8
9

case SHOP:
{

				Shop* newShop = new Shop(newPlayer);
				newShop->BeginShopping();
				delete newShop;
				break;
}

Topic archived. No new replies allowed.