我如何以不同的方式使用 CASE?

How I can use CASE in a different way?

您好,我正在尝试构建一个项目,但 case '2' 无法正常工作。

struct student
{
       string fname;//for student first name
       string lname;//for student last name
       string id;//for Registration No number
       string course;//for class info
}studentData;//Variable of student type

struct teacher
{
       string fname;//first name of teacher
       string lname;//last name of teacher
       string id;//Bool Group 
       string course;//Number of serves in School

}teach;//Variable of teacher type

int main()
{

   int i = 0, j;//for processing usage 
   char choice;//for getting choice
   string find;//for sorting
   string srch;

   while (1)//outer loop
   {
         system("cls");//Clear screen

      //Level 1-Display process 
         cout << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
         cout << "\n\n\t\t\tSCHOOL MANAGEMENT PROGRAM\n\n";
         cout << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
         cout << "\n\n\t\t\tMAIN SCREEN\n\n";
         cout << "Enter your choice: " << endl;
         cout << "1.Students information" << endl;
         cout << "2.Teacher information" << endl;
         cout << "3. Course information " << endl;
         cout << "4.  Display Strudent information  " << endl;
         cout << "5. Display Teacher Information " << endl;
         cout << "6. Display Course Information n" << endl;
         cout << "7.Exit program" << endl;
         cin >> choice;

         system("cls");//Clear screen


         switch (choice)//First switch
         {

         case '1': //Student
         {

                       {  ofstream f1("student.txt", ios::app);

                       for (i = 0; choice != 'n'; i++)
                       {

                             if ((choice == 'y') || (choice == 'Y') || (choice == '1'))
                             {
                                    cout << "Enter First name: ";
                                    cin >> studentData.fname;
                                    cout << "Enter Last name: ";
                                    cin >> studentData.lname;
                                    cout << "Enter Registration number: ";
                                    cin >> studentData.id;
                                    cout << "Enter class: ";
                                    cin >> studentData.course;

                                    f1 << studentData.fname << endl << studentData.lname << endl << studentData.id << endl << studentData.course << endl;
                                    cout << "Do you want to enter data: ";
                                    cout << "Press Y for Continue and N to Finish:  ";
                                    cin >> choice;
                             }
                       }
                       f1.close();
                       }
                       continue;//control back to inner loop -1



                continue;//Control pass to 1st loop    
         }

         case '2'://Teachers biodata
         {

                {  ofstream t1("teacher.txt", ios::app);

                for (i = 0; choice != 'n'; i++)
                {

                       if ((choice == 'y') || (choice == 'Y') || (choice == '1'))
                       {
                             cout << "Enter First name: ";
                             cin >> teach.fname;
                             cout << "Enter Last name: ";
                             cin >> teach.lname;
                             cout << "Enter Registration number: ";
                             cin >> teach.id;
                             cout << "Enter class: ";
                             cin >> teach.course;

                             t1 << teach.fname << endl << teach.lname << endl << teach.id << endl << teach.course << endl;
                             cout << "Do you want to enter data: ";
                             cout << "Press Y for Continue and N to Finish:  ";
                             cin >> choice;
                       }
                }
                t1.close();
                continue;//control back to inner loop -1

                }
                continue;//Control pass to 1st loop    
         }

         case '3':
         {
                {  ifstream f2("student.txt");

                cout << "Enter First name to be displayed: ";
                cin >> find;
                cout << endl;
                int notFound = 0;
                for (j = 0; (j < i) || (!f2.eof()); j++)
                {

                       getline(f2, studentData.fname);

                       if (studentData.fname == find)
                       {
                             notFound = 1;
                             cout << "First Name: " << studentData.fname << endl;
                             cout << "Last Name: " << studentData.lname << endl;

                             getline(f2, studentData.id);
                             cout << "Registration No number: " << studentData.id << endl;
                             getline(f2, studentData.course);
                             cout << "Class: " << studentData.course << endl << endl;
                       }

                }

                if (notFound == 0) {

                       cout << "No Record Found" << endl;
                }
                f2.close();
                cout << "Press any key two times to proceed";
                _getch();//To hold data on screen
                _getch();//To hold data on screen

                }
                continue;//control back to inner loop -1
         }
         case '4':
         {
                {  ifstream t2("teacher.txt");

                cout << "Enter First name to be displayed: ";
                cin >> find;
                cout << endl;
                int notFound = 0;
                for (j = 0; (j < i) || (!t2.eof()); j++)
                {

                       getline(t2, studentData.fname);

                       if (studentData.fname == find)
                      {
                             notFound = 1;
                             cout << "First Name: " << studentData.fname << endl;
                             cout << "Last Name: " << studentData.lname << endl;

                             getline(t2, teach.id);
                             cout << "Registration No number: " << studentData.id << endl;
                             getline(t2, teach.course);
                             cout << "Class: " << studentData.course << endl << endl;
                       }

                }

                if (notFound == 0) {

                       cout << "No Record Found" << endl;
                }
                t2.close();
                cout << "Press any key two times to proceed";
                _getch();//To hold data on screen
                _getch();//To hold data on screen

                }
                continue;//control back to inner loop -1
         }


         case '5':
         {}
         case '6':
         {}

         case '7':
         {
                break;//outer case 3
         }//outer case 3

         break;//outer loop
         }

   }
}

在您的 switch 语句中,在您检查 "choice" 是否为 '2 (case '2'://Teachers biodata) 的情况下,您还有另一个 if 语句检查 "choice" 是否为 ' 1',但此时 "choice" 将是 '2'。

if ((choice == 'y') || (choice == 'Y') || (choice == '1'))

变成

if ((choice == 'y') || (choice == 'Y') || (choice == '2'))