2013-08-01 67 views
0

我試圖重新建立一個收銀程序。 我有有一個名字僱員對象和折扣%卡訪問方法C++

staffMember me ("tom", 20); 

我要折扣申請總收銀的。如果我通過了折扣爲使用me.getDiscountPercent這樣

cashy.applyStaffDiscount(me.getDiscountPercent()); 

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

但是我想CIN的staffMember名字,所以我可以有不同折扣不同staffMembers整數參數我的方法的工作原理。我這樣做是不工作

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

方法:

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

感謝湯姆

+1

employee是一個沒有getDiscountPercent()方法的字符串。如果你有一個名字和折扣基地,然後寫一個方法,將名稱作爲參數並返回折扣。 –

+0

「employee」的數據類型是字符串。字符串類(來自std命名空間)沒有名稱爲「getDiscountPercent()」的任何方法。 – Jaywalker

+0

您可以將員工存儲在其姓名(字符串)是關鍵字的地圖中。 –

回答

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); 
2

參數employeestd::string,不是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()); 
}