좋은 하루for 루프를 동일한 출력에서 5 번 실행하려고 시도했습니다.
나는 직원을위한 급여 명세서를 출력하는 코드를 작성했습니다.
많은 연구를 했음에도 불구하고 (나는 스스로 알아 내려고 노력했다.) 동일한 출력 화면에서 다섯 명의 직원에 대한 정보를 연속적으로 입력 할 수 있도록 for 루프를 얻는 방법을 모르겠습니다. 프로그램을 실행하면 새로운 급여 전표의 시작 부분에있는 직원의 이름을 제외한 급여 전표에 대한 모든 정보를 입력 할 수 있습니다.
나는 가능한 한 많이 배우기를 원하는 초보자이므로 어떤 설명이라도 대단히 감사하겠습니다.
#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