我做我的程序如下:我不斷收到-2作爲我的更新工資
1)寫出與姓名和工資作爲員工的對象類名爲Employee類定義。該類包含兩個成員函數:構造函數和允許程序爲數據成員分配值的函數。
2)將兩個成員函數添加到Employee類。一個成員函數應該允許任何使用僱員對象的程序查看工資數據成員的內容。另一個成員函數應該允許程序查看員工姓名數據成員的內容。
3)將另一個成員函數添加到Employeeclass。成員函數應根據程序使用該對象提供的提高百分比來計算員工對新薪水的對象。在計算加權之前,成員函數應驗證加註百分比大於或等於零。如果提高百分比小於零,則成員函數應顯示錯誤消息。
4)編寫一個主函數,該函數將創建一個員工對象數組,向對象賦值,顯示所有對象的名稱和當前工資,詢問用戶提高百分比,然後計算並顯示所有對象的新薪水對象。
不過,我收到-2作爲我的新的工資後,我輸入的數據從鍵盤。我想另外一雙眼睛可以看到我不能做的事情,並且非常感謝有人能伸出援助之手,或者至少引導我走向正確的方向。也許這是一個邏輯錯誤,或者我的聲明有問題。感謝您的時間。
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class EMPLOYEE
{
public:
EMPLOYEE();//
EMPLOYEE(string name, int salary);//
public:
string name;//name to be input
int salary;//salary to be input
int percentage_raise;
int updated_salary;
public:
int enter_values();
int output_values();
int NEW_SALARY();
};
//default constructor
EMPLOYEE::EMPLOYEE()
{
name = "";
salary = 0;
}
//constructor with name/salary variables
EMPLOYEE::EMPLOYEE(string NAME, int SALARY)
{
name= NAME;
salary= SALARY;
}
//name and salary to be input...
int EMPLOYEE::enter_values()
{ cout<<"Enter name and salary: ";
cin>> name;
cin>>salary;
return 0;
}
//output
int EMPLOYEE::output_values()
{ cout<<"Name: "<<name<<endl;
cout<<"Salary: "<<salary<<endl;
return 0;
}
//
int EMPLOYEE::NEW_SALARY()
{
if (percentage_raise >= 0)
{ int updated_salary;
int raise= (salary *percentage_raise)/100;
updated_salary += raise;
}
else if(percentage_raise< 0)
{ cout<<"Error Message"<<endl;
}
return 0;
}
int main()
{
EMPLOYEE employees[100];
EMPLOYEE percent_to_be_raised;
int i;
for(i =0 ;i<100 ; i++)
{ employees[i]=EMPLOYEE();
employees[i].enter_values();
employees[i].name;
employees[i].salary;
// employees[i].NEW_SALARY();
employees[i].output_values();
cout<<"How much should the salary be raised by?"<<endl;
cin>>percent_to_be_raised.percentage_raise;
cout<<"-----------------------------"<<endl;
cout<<employees[i].name <<"'s new salary is "<<percent_to_be_raised.updated_salary<<endl;
}
}
請調試程序來縮小[詢問這裏]之前發生了什麼功問心無愧錯誤代碼(http://stackoverflow.com/help/dont-ask)! –
這裏有幾件事要修復。如果下面的答案不能指導OP,那麼我將刪除它。 –
@CPlusPlusOOAandD OP應該遵循我的建議,並逐步修復所有錯誤。你的答案可能會導致置於由OP評論無盡的後續問題: -/... –