美好的一天試圖獲得一個for循環執行5次相同的輸出
我已經寫了一個代碼,爲員工輸出薪資單。
此外,儘管做了大量的研究(我試圖弄清楚我自己),我不知道如何讓我的循環,讓我連續輸入5個不同的員工在同一輸出屏幕上的信息。當我運行該程序時,它允許我輸入工資單的所有信息,除了每個新工資單開始時的員工姓名。
我是一個初學者想盡可能多地學習,所以任何解釋將不勝感激。
我的代碼如下:
#include <iostream>
#include <string>
using namespace std;
void getData (string & theEmployee , float & theHoursWorked, float &
thePayRate)
{
cout<< "Enter the employees name and surname: "<< endl;
getline(cin, theEmployee);
cout << "Enter the numbers of hours the employee worked: " << endl;
cin >> theHoursWorked;
cout << "Enter the employees hourly pay rate?" << endl;
cin >> thePayRate;
}
float calculatePay(const string & theEmployee, float theHoursWorked, float
thePayRate)
{
float regularPay, thePay, overtimeHours;
if (theHoursWorked > 40)
{
regularPay = 40 * thePayRate;
overtimeHours = theHoursWorked - 40;
thePay = regularPay + (overtimeHours * 1.5 * thePayRate);
return thePay
}
else
thePay = theHoursWorked * thePayRate;
return thePay;
}
void printPaySlip(const string & theEmployee, float theHoursWorked, float
thePayRate, float thePay)
{
float overtimeHours;
cout << "Pay slip for " << theEmployee <<endl;
cout << "Hours worked: "<< theHoursWorked << endl;
if (theHoursWorked > 40)
overtimeHours = theHoursWorked - 40;
else
overtimeHours = 0;
cout << "Overtime hours: "<< overtimeHours << endl;
cout << "Hourly pay rate: " << thePayRate << endl;
cout << "Pay: " << thePay << endl;
cout << endl;
}
int main()
{
string theEmployee;
float theHoursWorked;
float thePayRate;
int thePay;
for (int i = 0; i < 5; i++)
{
getData(theEmployee, theHoursWorked, thePayRate);
thePay = calculatePay (theEmployee, theHoursWorked, thePayRate);
printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay);
}
return 0;
}
混合getline和cin >> int,請參閱http://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin或簡寫形式,嘗試把它放在getData的末尾:cin.ignore( std :: numeric_limits :: max(),'\ n'); –
如果我使用其他兩個下面的getline它說,有一個錯誤 – Geo
我將如何正確地聲明,但? – Geo