samedi 11 avril 2020

Compare function of string class not working as expected

I want to search professors based on their names. So i have to compare the name i am entering to the professor name. I tried debugging, what's happening is that its not going into the if(compare) statement. And I can't figure out why. Its a quite big code so I'm just including a part of it. "professors" is a vector of type Professor which is class.

        void RecruitProfessor()
        {
            Professor prof = Professor();

            string college,name,role,subject;
            cout<<"Enter the College Name: ";
            fflush(stdin);
            getline(cin, college);
            cout<<"Enter the Professor Name: ";
            getline(cin, name);
            fflush(stdin);
            cout<<"Enter the Professor Role: ";
            getline(cin, role);
            fflush(stdin);
            cout<<"Enter the Professor Subject: ";
            getline(cin, subject);
            fflush(stdin);


            prof.setCollegeName(college);
            prof.setProfessorName(name);
            prof.setProfessorRole(role);
            prof.setProfessorSubject(subject);
            professors.push_back(prof);
        }

        void DisplayProfessorInfo()
        {
            bool returnToMainMenu = false;
            while(!returnToMainMenu)
            {
                cout<<"1. Fetch the details of all the professors \n";
                cout<<"2. Fetch the details of professors by Name \n";
                cout<<"3. Fetch the details of professors by Role \n";
                cout<<"4. Fetch the details of professors by Subject \n";
                cout<<"5. Fetch the details of professors by Colleges \n";
                cout<<"6. Return to main menu \n";

                Professor prof = Professor();
                int choice;
                cout<<"Please enter your choice:\n";
                choice = getInt();
                switch(choice)
                {
                    case 1:
                    {
                        if(professors.size()==0)
                        {
                            cout<<"\n\nNo professors found!\n\n";
                            returnToMainMenu = true;
                        }
                        else
                        {
                            for(Professor i:professors)
                            {
                                //cout << i.getProfessorCollege() << endl;
                                cout<<"Professor College: " + i.getProfessorCollege() + "\n";
                                cout<<"Professor Name: " + i.getProfessorName() + "\n";
                                cout<<"Professor Role: " + i.getProfessorRole() + "\n";
                                cout<<"Professor Subject: " + i.getProfessorSubject() + "\n";
                                cout<<"\n\n";
                            }
                        }
                        break;
                    }
                    case 2:
                    {
                        string name;
                        if(professors.size()==0)
                        {
                            cout<<"\n\nNo professors sfound!\n\n";
                            returnToMainMenu = true;
                        }
                        else
                        {
                            getline(cin, name);
                            fflush(stdin);
                            cin.ignore(numeric_limits<streamsize>::max(), '\n');
                            //cout<<name;
                            for(Professor i:professors)
                            {
                                if((name.compare(i.getProfessorName()))==0)
                                {
                                    cout<<"Professor College: " + i.getProfessorCollege() + "\n";
                                    cout<<"Professor Name: " + i.getProfessorName() + "\n";
                                    cout<<"Professor Role: " + i.getProfessorRole() + "\n";
                                    cout<<"Professor Subject: " + i.getProfessorSubject() + "\n";
                                    cout<<"\n\n";
                                }
                                else{continue;}
                            }
                        }
                        break;
                    }

Aucun commentaire:

Enregistrer un commentaire