-1

Hello I am trying to build a project but the case '2' is not working.

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
         }

   }
}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • What do you mean they're "not working"? – Asteroids With Wings May 18 '20 at 14:32
  • When I choose case 2 the screen go black, doen't show the options for case 2 – Byron Arriola May 18 '20 at 14:32
  • 1
    This is a perfect opportunity to learn how to use a debugger. If you go through your code step by step and examine values of your variables, you should see where the issue is. – Yksisarvinen May 18 '20 at 14:37
  • I was thinking the problem is with the char variable that I switch, but if I change it to int stops working, I am not sure where I am making the mistake – Byron Arriola May 18 '20 at 14:40
  • Please edit your post: explain why they are not working. Compiling error? nothing shows up? something else? – Roim May 18 '20 at 14:40
  • @Yksisarvinen This and it's also a prefect opportunity to learn how to structure your code. Try splitting distinct functionality of your program into separate functions. Comprehending 30 or so lines is always simpler than trying to reason about a mountain range like that. It also makes the aforementioned debugging easier. – Big Temp May 18 '20 at 14:41
  • For case 2, you can only exit the loop if at first `(choice == 'y') || (choice == 'Y') || (choice == '1')` and you then input `'n'`. But `choice` is `'2'` when you enter the loop, so... – molbdnilo May 18 '20 at 14:41
  • ***I was thinking the problem is with the char variable that I switch, but if I change it to int stops working, I am not sure where I am making the mistake*** This is exactly the reason to use a debugger. If you knew how to use one you would not have to guess what the problem is. You would likely be able to track down and see the exact problem by stepping through your code 1 line at a time looking at the variables after each step. – drescherjm May 18 '20 at 14:43
  • if you are using Visual Studio I highly recommend you spend 20 minutes and follow this tutorial: [https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019) other debuggers like `gdb` will have similar abilities. – drescherjm May 18 '20 at 14:47
  • From your title, to use `cases` in a different way, search the internet for "C++ Duffs device". – Thomas Matthews May 18 '20 at 14:48
  • Some `switch/case` statements can be replaced by a lookup table (array). – Thomas Matthews May 18 '20 at 14:49
  • Search for `std::tolower` and `std::toupper` in your favorite reference. These functions allow you to convert to one case (lower or upper), so you only need to make one comparison, not two. – Thomas Matthews May 18 '20 at 14:50

1 Answers1

1

inside your switch statement, inside the case where you are checking if "choice" is '2 (case '2'://Teachers biodata), you have yet another if statement which checks if "choice" is '1', but at that point "choice" will be '2'.

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

becomes

if ((choice == 'y') || (choice == 'Y') || (choice == '2'))
tb044491
  • 144
  • 7