so i'm getting this error: Error 14 error C2360: initialization of 'levelTwo' is skip

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Temper
    New Member
    • Apr 2014
    • 2

    so i'm getting this error: Error 14 error C2360: initialization of 'levelTwo' is skip

    i"m new at this.. please help me! i keep getting this error Error 5 error C2360: initialization of 'levelOne' is skipped by 'case' label.
    I get a "transfer of control bypasses initialization of:" error when i try to build the following switch:



    Code:
    int SelectCourses()
    {
    	int choice=0;
    	string course;
    
    	cout<<"Enter Level"<<endl;
    	cin>> choice;
    
    	ofstream MyCourses;
    	MyCourses.open("Courses.txt",ios::out);
    
    	if (!MyCourses)
    	{
    		ofstream ("Courses.txt",ios::out);
    	}
    	else
    	{
    		cerr<<"failed to find file"<<endl;
    		(EXIT_FAILURE);
       }
    	switch(choice)
    	{
    	case 1: ifstream levelOne;
    		levelOne.open("Level1.txt");
    		cout<<"Select any  4 or 4 modules from the list provided"<<endl;
    		cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;
    	case 2: ifstream levelTwo;
    		levelTwo.open("level2.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;
    	case 3: ifstream levelThree;
    		levelThree.open("level3.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;
    
    	case 4: ifstream levelFour;
    		levelFour.open("level4.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;
    
    	case 5: ifstream levelFive;
    		levelFive.open("level5.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;
    
    	default: cout << "Invalid option please try again" << endl;//when an incorrect option is entered
    							break;
    
    							MyCourses.close();
    				
    	}
    }
    Last edited by weaknessforcats; Apr 7 '14, 05:43 AM. Reason: added code tags
  • Temper
    New Member
    • Apr 2014
    • 2

    #2
    i solved it thou..thanks alot for not helping..
    but for the sake of someone who is in a bind; such as my previous situation

    solution is:

    Code:
    			switch(choice)
    	{
    	case 1: {ifstream levelOne;
    		levelOne.open("Level1.txt");
    		cout<<"Select any  4 or 4 modules from the list provided"<<endl;
    		cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;}
    	case 2:{ ifstream levelTwo;
    		levelTwo.open("level2.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;}
    	case 3: {ifstream levelThree;
    		levelThree.open("level3.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;}
    
    	case 4: {ifstream levelFour;
    		levelFour.open("level4.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;}
    
    	case 5: {ifstream levelFive;
    		levelFive.open("level5.txt");
    		cout<<"Select any  4 or 5 modules from the list provided:"<<endl;
    						cout<<"Option 1:"<<endl;"/t";
    						cout<<"Option 2:"<<endl;"/t";
    						cout<<"Option 3:"<<endl;"/t";
    						cout<<"Option 4:"<<endl;"/t";
    						cout<<"Option 5:"<<endl;"/t";
    						cin>>course;
    						break;}
    
    	default:{ cout << "Invalid option please try again" << endl;//when an incorrect option is entered
    		break;}
    
    							MyCourses.close();
    				
    	}
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Probably you don't need the braces. I suspect the case line must end with the colon. Just move the code after the colon to the next line.

      The brace forces a change of scope and would act like a newline key.

      It's like the #include. You can't put anything on those lines either.

      Comment

      Working...