Switch using array trouble...

I'll start with the code in a class member function removeFromList:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  switch (option) {
			case 'c':
			case 'C':
				strcpy(tempArray, " ");
				list[index].setTitle(tempArray);
				//outputFileData function removes index song with empty title cstring  
				outputFileData();
				size--;
				strcpy(tempArray, "songs.txt");
				songlist(tempArray); //update array with removed index song
				break;
				
			case 'm':
			case 'M':
				return;
			case 'r':
			case 'R':
				break;
			default:
				cout << "Invalid option please try again.";
				continue;


If I try and run this code, I get a compile error stating:

g++ -c songlist.cpp -o songlist.o
songlist.cpp: In member function ‘void songlist::removeFromList()’:
songlist.cpp:297:9: error: jump to case label [-fpermissive]
case 'm':
^
songlist.cpp:294:14: note: crosses initialization of ‘songlist tempArray’
songlist(tempArray); //update array with removed index song
^
songlist.cpp:298:9: error: jump to case label [-fpermissive]
case 'M':
^
songlist.cpp:294:14: note: crosses initialization of ‘songlist tempArray’
songlist(tempArray); //update array with removed index song
^
songlist.cpp:300:9: error: jump to case label [-fpermissive]
case 'r':
^
songlist.cpp:294:14: note: crosses initialization of ‘songlist tempArray’
songlist(tempArray); //update array with removed index song
^
songlist.cpp:301:9: error: jump to case label [-fpermissive]
case 'R':
^
songlist.cpp:294:14: note: crosses initialization of ‘songlist tempArray’
songlist(tempArray); //update array with removed index song
^
songlist.cpp:303:4: error: jump to case label [-fpermissive]
default:
^
songlist.cpp:294:14: note: crosses initialization of ‘songlist tempArray’
songlist(tempArray); //update array with removed index song
^
Makefile:11: recipe for target 'songlist.o' failed
make: *** [songlist.o] Error 1

Now I've done some research and I figured out by placing curly braces around the statements belonging to case: 'c':, that this will fix this compile time error. It has something to do with the scope of tempArray, but I still don't quite understand why and how? If someone could explain this I would really appreciate it, because I'm not happy just fixing the code and not understand why.

Thank you!

Show the entire relevant code. Since the compiler has problems with 'songlist tempArray' show everything around that line.

If you want to define a variable like 'songlist tempArray' within a case statement you need to put it in { } because in c++ it is illegal to skip a variable definition.
Why is it illegal? Is it because I declared a variable but ended up not defining it outside of the switch structure? What if I define it when I initially declare the variable and skip it's second definition in the switch structure?
Topic archived. No new replies allowed.