當我爲員工數輸入一個小數時,它輸出「請輸入一個atleast 1的值」無限次數。但不應該將小數點截斷?我如何解決這個問題,所以小數點被刪除。這發生在daysMissed的輸入中。循環中的C++函數
#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;
while(employeeNum < 1)
{
cout << "Please enter a value of atleast 1 " << 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;
while(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;
}
您可能還想擺脫冒犯的字符或行。 – chris 2013-03-26 01:55:33
@chris相當真實!忘了忽略。 – Serdalis 2013-03-26 01:59:20
它回答這個問題:「但不應該小數點被截斷?」 – 4pie0 2013-03-26 02:06:44