【问题标题】:How can I get the information from user instead of using specific values?如何从用户那里获取信息而不是使用特定值?
【发布时间】:2020-08-17 20:26:41
【问题描述】:

已编辑。你好,我是这里的新手,也是 C++ 的新手。我想编写一个可以基本使用函数和类的代码。我在主函数中询问了用户 ID、姓名、薪水。顺便说一句,在编译之前没有发生错误,程序询问了用户 ID,但是跳过了我用字符串定义的名称。然后继续取薪水。为什么它不取字符串值?在代码的末尾我添加了输出

enter code here
 #include<string>
 #include<iostream>
 using namespace std;

 class Employee
  {
   private:
   int eid;
   string name;
   public:
   Employee(int e,string n)
   {
    eid=e;
    name=n;
    setEmployeeID(e);
    setName(n);
    }
    void setEmployeeID(int e)
    {
     eid=e;
    }
     int getEmployeeID()
    {
     return eid;
    }
     void setName(string n)
    {
     name=n;
     }
     string getName()
     {
     return name;
     }
     };
 class FulltimeEmployee:public Employee
     {
     private:
        int salary;
     public:
        FulltimeEmployee(int e,string n,int sal):Employee(e,n)
      {
      salary=sal;
      setSalary(sal);
       }
      void setSalary(int sal)
       {
      salary=sal;
       }
     int getSalary()
       {
     return salary;
        }
        };
class ParttimeEmployee:public Employee
     {
     private:
     int wage;
     public:
     ParttimeEmployee(int e,string n,int w):Employee(e,n)
     {
     wage=w;
     setWage(w);
     }
     void setWage(int w)
     {
      wage=w;
      }
      int getWage()
     {
     return wage;
      }
      };
int main()
{
int workerId;
string workerName;
int workerPayment;
cout<<"Please enter your ID please: ";
cin>>workerId;
cout<<"Please enter your name please: ";
cout<<"Please enter your salary: ";
getline(cin,workerName);
cin>>workerPayment;
ParttimeEmployee personel1(workerId,workerName,workerPayment);
FulltimeEmployee personel2(workerId,workerName,workerPayment);;
cout<<"Daily wage of "<<personel1.getName()<<" is "<<" "   <<personel1.getWage()<<endl;
cout<<"Worker ID is:"<<personel1.getEmployeeID()<<endl;
cout<<"Salary of "<<personel2.getName()<<"is "<<" "<<personel2.getSalary()<<endl;
  cout<<"Worker ID is:"<<personel2.getEmployeeID()<<endl;
  return 0;

  }


enter code here
Here is my output:
Please enter your ID please: 12123
Please enter your name please: Please enter your salary: 1000
Daily wage of  is  1000
Worker ID is:12123
Salary of is  1000

【问题讨论】:

  • 我建议展示您尝试过的内容。当您将错误的东西排除在外时,很难帮助您解决错误。
  • 考虑“告诉,不要问”,“这提醒我们将行为放在对象本身内部(使用相同的字段)。”,因此,“cout

标签: c++ function class getter-setter


【解决方案1】:

我在猜测,但你可以使用cin

class Employee
{
//...
public:
     void get_info_from_user()
     {
         std::cout << "Enter employee ID: ";
         std::cin >> eid;
         std::cout << "Enter employee name: ";
         std::getline(std::cin, name);
     };
};

您也可以添加与其他类类似的内容。

编辑1:重载运算符>>
为了更通用,你可以重载operator&gt;&gt;:

class Employee
{
//...
  public:
      friend std::istream& operator>>(std::istream& input, Employee& e);
};

std::istream& operator>>(std::istream& input, Employee& e)
{
    input >> e.eid;
    std::getline(input, e.name);
    return input;
}

上面的代码允许你做这样的事情:

std::vector<Employee> database;
Employee e;
while (data_file >> e)
{
    database.push_back(e);
}

【讨论】:

  • 说实话。这并不是一个疯狂的猜测。可以使用更多的妖精。
猜你喜欢
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 2020-05-22
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多