无法将 "name " 作为输入

Not able to take "name "as input

我认为存在某种类型问题。也许我错过了什么。当我输入选项 1 时,我得到 "Enter the Name: Enter the Role: " 无法将 "name" 作为输入。我创建了一个 class "Professor" 数据成员作为 3 个字符串类型的向量和一个 "int count" 以及三个成员函数 "Recruit"、"DisplayInfo" 和 "Remove"。我基本上是想建立一个大学教授的数据。

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>

    using namespace std;

    class Professor
    {
    private:
        vector<string> Name;
        vector<string> Role;
        vector<string>Subject;
        static int count1;
    public:
        void Recruit()
        {
            string name,role,subject;
            cout<<"Enter the Name: ";
            getline(cin, name);
            Name.push_back(name);

            cout<<"Enter the Role: ";
            getline(cin, role);
            Role.push_back(role);

            cout<<"Enter the Subject to be assigned: ";
            getline(cin, subject);
            Subject.push_back(subject);
            count1++;
        }
        void DisplayInfo()
        {
            //cout<<"\nName   Role   Subject\n";
            for(int i=0; i<count1; i++)
            {
                cout<<"Name: "<<Name[i]<<"\n";
                cout<<"Role: "<<Role[i]<<"\n";
                cout<<"Subject: "<<Subject[i]<<"\n";   
            }
            cout<<"\n\n";
        }
        void Remove()
        {
            string name, role, subject;
            cout<<"Enter the name of the Professor, his/her Role and the subject assigned to him/her: ";
            cin>>name>>role>>subject;
            for(int i=0; i<count1; i++)
            {
                if(Name[i]==name && Role[i]==role && Subject[i]==subject)
                {
                    vector<string>::iterator iter1 = find(Name.begin(), Name.end(), name);

                    if(iter1 == Name.end())
                    {
                        cout<<"";
                    }
                    else{
                        Name.erase(iter1);
                    }
                    vector<string>::iterator iter2 = find(Role.begin(), Role.end(), role);
                    if(iter2 == Role.end())
                    {
                        cout<<"";
                    }
                    else{
                        Role.erase(iter2);
                    }
                    vector<string>::iterator iter3 = find(Subject.begin(), Subject.end(), subject);
                    if(iter2 == Role.end())
                    {
                        cout<<"";
                    }
                    else{
                        Role.erase(iter2);
                    }
                }
            }
        }
    };

    int Professor :: count1;

    int main()
    {
        Professor p;
        int choice;
        do{
            cout<<"You have the following Choices:\n";
            cout<<"1) Recruit a Professor\n";
            cout<<"2) Display the Information about Professors\n";
            cout<<"3) Remove a Professor\n";
            cout<<"4) Quit\n";
            cout<<"\n\nEnter the choice:";

            cin>>choice;/*This is where it occurs. Code for "Recruit" member function is defined in 
                         "Professor" class above.*/

            switch(choice)
            {
                case 1:
                {
                    p.Recruit();
                    break; 
                }
                case 2:
                {
                    p.DisplayInfo();
                    break;
                }
                case 3:
                {
                    p.Remove();
                    break;
                }
                case 4:
                {
                    break;
                }
                default:
                {
                    cout<<"Sorry Wrong Choice!!\n";
                }
            }
        }while(choice!=4);
        return 0;
    }

嗨,abhishek,问题出在 getline 上。我建议您阅读有关 getline 的使用,现在只需在 Recruit() 函数中添加以下行,您的问题就会得到解决

    void Recruit()
    {
        string name,role,subject;
        cout<<"Enter the Name: ";
        getline(cin, name);
        cin.ignore();
        Name.push_back(name);

        cout<<"Enter the Role: ";
        getline(cin, role);
        cin.ignore();
        Role.push_back(role);

        cout<<"Enter the Subject to be assigned: ";
        getline(cin, subject);
        cin.ignore();
        Subject.push_back(subject);
        count1++;
    }

养成在 getline 之后使用 cin.ignore 的习惯。 快乐编码