Error: crosses initialization of 'int choice'

Pages: 12
Additionally I recieve the error: jump to case label.

(using the code::blocks IDE)

Here is the code where I think the error is in, I know it is probably something simple but I cant see it:

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
            {
                //The Error!?
                int choice = 0;

                while(choice != 8)
                {
                cout << "----LOCATIONS----" << endl;
                cout << "(please select one of the below locations to learn more)\n" << endl;
                cout << "1: Nunga" << endl;
                cout << "2: Tiagor" << endl;
                cout << "3: Alhari Desert" << endl;
                cout << "4: Estess" << endl;
                cout << "5: Yovania" << endl;
                cout << "6: The Great Ocean" << endl;
                cout << "7: The frozen wastes" << endl;
                cout << "8: Read some history(from history you can return to main)\n" << endl;

                cin >> choice;

                cout << endl;

                switch (choice)


                case 1:
                {
                    cout << "----NUNGA----" << endl;
                    cout << "Nunga is a country in the South-West of the eastern hemisphere" << endl;
                    cout << "of Mystice. It is one of the few countries that didn't\n succumb to the darkness of Yovania(see Locations #5: Yovania)." << endl;
                    cout << "Nunga is one of the superpowers of Mystice.\n The country is mainly affiliated with the element Earth & the goddess Terra." <<endl;
                    cout << "Several great people have come from Nunga, mainly White Wizards." << endl;
                    cout << "Xhalite & Zyaji Dirkrest were both from Nunga. Kathruia Ahenin was from Nunga as well." << endl;
                    cout << "(for more information on these people, go to history->famous/infamous historical figures)" << endl;
                    cout << "Nunga has a very well organized and powerful military.\n Demonstrated by that fact that it beats back Yovanian forces each time!" << endl;
                    cout << "The country is so named for its native magical creature called a nungun" << endl;
                    cout << "Nunga was founded thousands of years ago by Levonian men.\n Levonia is now the dark realm of Yovania.\nLearn more in history section." << endl;
                    cout << "The capitol city is Port Serene at the very North-end of the Bay of Life" << endl;
                    break;
                }
                case 2:
                {
                    cout << "----TIAGOR----" << endl;
                    cout << "An island chain of mystical power...\n filled with water elemental energy." << endl;
                    cout << "Located North of Nunga & East of the Alhari Desert. Home to the white wizard Kino'oh." << endl;
                    cout << "It takes up the entire North-Eastern seaboard\n of the western continent above Nunga," << endl;
                    cout << "plus the huge chain of islands extends far East into the Great Ocean" << endl;
                    cout << "with dagger &  tearing claw islands at the top near the Artic Waste & the Whirling Seas." << endl;
                    break;
                }


As you can see I am writing a guide to a fantasy world. Please help thank you!
That error happens when you declare variables inside of switch cases, as it's possible to not fall into that case and then hit the end of the block, destructing the variable that wasn't even constructed.
Can you tell me how to fix it?
substitue this line:

1
2
//The Error!?
int choice = 0;


for this

1
2
//No error!
choice = 0;


and declare int choice; outside of any switch statements.
I did that but then...

Ln 176 error: duplicate case value
Ln 65 error: previously used here

But it isn't the same! I need case 2 to display info!

Any suggestions?
Show the entire switch statement please.
Only has case 1 and 2 since I havent worked on others yet:

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
 int choice;
            {
                //No Error?
                choice = 0;

                while(choice != 8)
                {
                cout << "----LOCATIONS----" << endl;
                cout << "(please select one of the below locations to learn more)\n" << endl;
                cout << "1: Nunga" << endl;
                cout << "2: Tiagor" << endl;
                cout << "3: Alhari Desert" << endl;
                cout << "4: Estess" << endl;
                cout << "5: Yovania" << endl;
                cout << "6: The Great Ocean" << endl;
                cout << "7: The frozen wastes" << endl;
                cout << "8: Read some history(from history you can return to main)\n" << endl;

                cin >> choice;

                cout << endl;

                switch (choice)


                case 1:
                {
                    cout << "----NUNGA----" << endl;
                    cout << "Nunga is a country in the South-West of the eastern hemisphere" << endl;
                    cout << "of Mystice. It is one of the few countries that didn't\n succumb to the darkness of Yovania(see Locations #5: Yovania)." << endl;
                    cout << "Nunga is one of the superpowers of Mystice.\n The country is mainly affiliated with the element Earth & the goddess Terra." <<endl;
                    cout << "Several great people have come from Nunga, mainly White Wizards." << endl;
                    cout << "Xhalite & Zyaji Dirkrest were both from Nunga. Kathruia Ahenin was from Nunga as well." << endl;
                    cout << "(for more information on these people, go to history->famous/infamous historical figures)" << endl;
                    cout << "Nunga has a very well organized and powerful military.\n Demonstrated by that fact that it beats back Yovanian forces each time!" << endl;
                    cout << "The country is so named for its native magical creature called a nungun" << endl;
                    cout << "Nunga was founded thousands of years ago by Levonian men.\n Levonia is now the dark realm of Yovania.\nLearn more in history section." << endl;
                    cout << "The capitol city is Port Serene at the very North-end of the Bay of Life" << endl;
                    break;
                }
                case 2:
                {
                    cout << "----TIAGOR----" << endl;
                    cout << "An island chain of mystical power...\n filled with water elemental energy." << endl;
                    cout << "Located North of Nunga & East of the Alhari Desert. Home to the white wizard Kino'oh." << endl;
                    cout << "It takes up the entire North-Eastern seaboard\n of the western continent above Nunga," << endl;
                    cout << "plus the huge chain of islands extends far East into the Great Ocean" << endl;
                    cout << "with dagger &  tearing claw islands at the top near the Artic Waste & the Whirling Seas." << endl;
                    break;
                }


Then there is another before it with the same type of switch statement. The only thing different is the displayed information. In fact my whole program is a huge switch statement with other switch statements within making sub sections of the guide to the land of Mystice.
Last edited on
Sounds like it would be a better idea to split each section into a function or something.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
int main() {
    int choice;
    std::cout<<"blah blah";
    std::cin>>choice; //no error checking
    switch(choice) {
         case 1:
             history(); break;
         case 2:
             people(); break;
         //...
    }
}
Your switch needs braces. It could be that which is causing the error if you are nesting them.
@firedraco

I might try that, but I think I need the while loop to be able to go back to the main section.

@Zhuge

Okay, where does it need braces? There's so many it's hard to tell.
Line 23~50. But that's why you should put it in a function ;) If you need to go back to the main loop, you can just return from the function and have a loop around the entire int choice stuff.
1
2
3
4
5
6
7
8
9
switch (arg) {
          case 1st: //...
                         break;
          case 2nd: //...
                          break;
          case nth: //...
                          break;
          default: //do something...
}
Here is the fixed version of your program.

The error in the braces


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
57
#include<iostream>
using namespace std;
int main()
{
                
                int choice = 0;

                while(choice != 8)
                {
                cout << "----LOCATIONS----" << endl;
                cout << "(please select one of the below locations to learn more)\n" << endl;
                cout << "1: Nunga" << endl;
                cout << "2: Tiagor" << endl;
                cout << "3: Alhari Desert" << endl;
                cout << "4: Estess" << endl;
                cout << "5: Yovania" << endl;
                cout << "6: The Great Ocean" << endl;
                cout << "7: The frozen wastes" << endl;
                cout << "8: Read some history(from history you can return to main)\n" << endl;

                cin >> choice;

                cout << endl;

                switch (choice)
				{   //modify here 

                case 1:
                {
                    cout << "----NUNGA----" << endl;
                    cout << "Nunga is a country in the South-West of the eastern hemisphere" << endl;
                    cout << "of Mystice. It is one of the few countries that didn't\n succumb to the darkness of Yovania(see Locations #5: Yovania)." << endl;
                    cout << "Nunga is one of the superpowers of Mystice.\n The country is mainly affiliated with the element Earth & the goddess Terra." <<endl;
                    cout << "Several great people have come from Nunga, mainly White Wizards." << endl;
                    cout << "Xhalite & Zyaji Dirkrest were both from Nunga. Kathruia Ahenin was from Nunga as well." << endl;
                    cout << "(for more information on these people, go to history->famous/infamous historical figures)" << endl;
                    cout << "Nunga has a very well organized and powerful military.\n Demonstrated by that fact that it beats back Yovanian forces each time!" << endl;
                    cout << "The country is so named for its native magical creature called a nungun" << endl;
                    cout << "Nunga was founded thousands of years ago by Levonian men.\n Levonia is now the dark realm of Yovania.\nLearn more in history section." << endl;
                    cout << "The capitol city is Port Serene at the very North-end of the Bay of Life" << endl;
                    break;
                }
                case 2:
                {
                    cout << "----TIAGOR----" << endl;
                    cout << "An island chain of mystical power...\n filled with water elemental energy." << endl;
                    cout << "Located North of Nunga & East of the Alhari Desert. Home to the white wizard Kino'oh." << endl;
                    cout << "It takes up the entire North-Eastern seaboard\n of the western continent above Nunga," << endl;
                    cout << "plus the huge chain of islands extends far East into the Great Ocean" << endl;
                    cout << "with dagger &  tearing claw islands at the top near the Artic Waste & the Whirling Seas." << endl;
                    break;
                }
				} //modify here
				
				} //modify here
				return 0;
}
@Baso: Don't just fix programs, let the OP do it himself. He'll probably learn more that way.

Btw, Xhalite, did you make all this lore up yourself?
@firedraco well I have a friend to help me since he makes up some of this stuff as well. This console application is really only meant as documentation for a bigger harder 3D game project my friends and I are working on. Yeah I guess I made up some of this lore.
dammit! I can't reply with my entire source code because it is too long and I know that would help me and you in figuring this out. I added the brackets and just got more confused not being able to find which brackets are which and then I can't compile with error: expected unqualified id before return.BUT I NEED THAT BRACKET TO TIE EVERYTHING TOGETHER SINCE IT IS THE int main() bracket!
@firedraco I usually can fix problems by myself, but some of them are baffling!
Okay I put it in functions like firedraco suggested. It compiles and runs but is now buggier than ever and doesn't loop back to main options but instead closes when I press the integer for return to main and gives me this warning several times: statement has no effect.

So, what do I do now to get it back to what it was doing before this whole mess started??
If you could, try putting your code up on pastebin or something (since it seems there is a lot of it).
I have an idea as to how to fix this buggy mess of a program. I figured that I could call a function to loop it back to the main options rather than a while loop?? Any idea what to do?
Pages: 12