Switch error

closed account (DSvMDjzh)
I'm using the following code in my program to exit, save, and stop/start but i get an error saying "Transfer of control bypasses initialization of." Why is it doing that and how can i fix it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	case WM_KEYDOWN:
		switch ( wParam )
		
		{	
		case VK_ESCAPE:
			//Quit
			PostQuitMessage(0);
			return 0;
		case VK_F1:
			//Save
			ofstream myfile;
			myfile.open ("array.txt");						
			for(int i=0;i<65000;i++){
			myfile << Data[i][0];
			myfile << " ";
			myfile << Data[i][1];
			myfile << "\n";
			}
			myfile.close();
			return 0;
		case VK_F2:
			//Stop Timer code will go here
		return 0;

Thanks
Josh
When you declare a variable in a switch, you need to wrap it scope with braces to avoid jumps passed its initialization
eg:
1
2
3
4
5
6
case VK_F1:
{
    ofstream myfile;
    //...
}
case VK_F2:
closed account (DSvMDjzh)
I see,
Thanks very much
i'm about to do my assingment for switch case..
and i have to explain about switch case diz friday..
but i don't know anything about switch case..
how to make a program and how to use it..
help me plz..
make a forum post and you'll most likely get some answers
anyway to answer your question....

a switch statement usually compares a variable to a few different cases like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int options;

    cout << "Please select 1 of the following options to continue" << endl:
    cout << "1. Do this function" << endl;
    cout << "2. do this function" << endl;
    etc.....
    cin >> option;

    switch ( option )
    {
         case 1: do this code;

         break;
        
         case 2: do this code;

         etc....
     }

return 0;
}
    


also check out the tutorials section.. probly more helpful.
Last edited on
tq jammas..but i did'nt really understand..huhuhuhu i'm sorry..
Topic archived. No new replies allowed.