在我的程序中,它首先询问员工人数。if 循环表示如果输入是输入 < 1,则输出说“请输入至少 1 的值”。如果您输入 0 或 -1,它会输出供用户更正,但它只执行一次。因此,如果用户输入 -1 两次,它将运行第二次。它也使用第二个 if 循环来执行此操作。
#include <iomanip>
#include <iostream>
using namespace std;
int getNumEmployees();
int getDaysMissed(int);
double getAverageDays(int,int);
int main()
{
int numEmployees;
int daysMissed;
double average;
//Get the number of employees
numEmployees = getNumEmployees();
//Get the number of days missed
daysMissed = getDaysMissed(numEmployees);
//Get the average days missed
average = getAverageDays(numEmployees, daysMissed);
cout << "The average number of days missed is: " << average << endl;
system("pause");
return 0;
}
int getNumEmployees()
{
int employeeNum;
cout << "Enter the number of company employees: ";
cin >> employeeNum;
if(employeeNum < 1)
{
cout << "Please enter a value greater than 0 " << endl;
cin >> employeeNum;
}
return employeeNum;
}
int getDaysMissed(int employeeNum)
{
int totalDays = 0;
int employee;
for(int count = 1; count <= employeeNum; count++)
{
cout << "Enter the number of days employee " << count << " missed: ";
cin >> employee;
if(employee < 0)
{
cout << "Enter a positive number for days missed " << endl;
cin >> employee;
}
totalDays += employee;
}
return totalDays;
}
double getAverageDays(int employeeNum, int totalDays)
{
double averageDays;
averageDays = totalDays / employeeNum;
return averageDays;
}