【问题标题】:Not able to take "name "as input无法将“名称”作为输入
【发布时间】:2020-07-23 05:23:09
【问题描述】:

我认为存在某种类型的问题。也许我错过了一些东西。当我将选项输入为 1 时,我提供了“输入名称:输入角色:”,无法将“名称”作为输入。我创建了一个“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;
    }

【问题讨论】:

  • 您的问题似乎已经有了正确答案。有点题外话,但我建议你重新组织你的代码。将“教授”类重命名为“大学”类,然后使用字段名称、角色和主题创建一个名为“教授”的结构可能更有意义。这样,您可以拥有一个 std::vector 对象,而不是拥有三个不同的 std::vector 对象。正如您现在所拥有的那样,“创建教授”然后使用该教授来招聘教授和移除教授在逻辑上是没有意义的。
  • 另外,您不需要静态 int count1 变量。如果要获取教授的数量,可以调用“name.size()”、“role.size()”或“subject.size()”(它们的大小应该相同)。

标签: c++ vector input switch-statement getline


【解决方案1】:

嗨,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 的习惯。 快乐编码

【讨论】:

  • 谢谢...但还是一样。我仍然面临同样的问题。
  • 我期待你会选择提示,无论如何我已经编辑了我的回复,试试吧。会工作的。
  • 不,我确实选择了提示并做了和你一样的事情。但它仍然没有发生。
  • 是的,我很抱歉......它确实有效,谢谢。实际上我正在使用 Visual Studio,可能存在一些无法保存的缓存问题。我在代码块上尝试了同样的方法,它正在工作,所以我重新启动了 Visual Studio。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
相关资源
最近更新 更多