0

我正在尝试重新创建收银机程序。我有一个员工对象,它有一个名字和一个折扣 %

staffMember me ("tom", 20);

我想对收银台的总额应用折扣。如果我像这样使用 me.getDiscountPercent 将折扣作为整数参数传递,我的方法就有效

cashy.applyStaffDiscount(me.getDiscountPercent());

void cashRegister::applyStaffDiscount(int discount){
    total = (total/100)*(100-discount);
}

但是我想输入staffMember 名称,这样我就可以拥有不同折扣的不同staffMember。我做了这行不通

string employee;
cout << "Enter name: ";
cin >> employee;
cashy.applyStaffDiscount(employee);

方法:

void cashRegister::applyStaffDiscount(string employee){
total = (total/100)*(100-employee.getDiscountPercent());
}

谢谢汤姆

4

2 回答 2

2

参数employee是 a std::string,而不是 a staffMember。在您的 applyStaffDiscount 函数中,您必须传递一个员工,而不是一个字符串:

string employee_name;
int employee_discount;
cout << "Enter employee data: ";
cin >> employee_name;
cin >> employee_discount;

staffMember employee( employee_name , employee_discount); //Staff variable

cashy.applyStaffDiscount(employee);

/* ... */

void cashRegister::applyStaffDiscount(const staffMember& employee)
{
    total = (total/100)*(100-employee.getDiscountPercent());
}
于 2013-08-01T10:43:13.190 回答
2

的数据类型employee是字符串。该类string(来自std命名空间)没有任何名为 的方法getDiscountPercent()。也许,您想要做的是:

string name;
int discount;

cout << "Enter name: ";
cin >> name;

cout << "Enter discount: ";
cin >> discount;

staffMember employee (name, discount); // <-- this is what you really want!

cashy.applyStaffDiscount(employee);
于 2013-08-01T10:39:02.613 回答