0
在我的程序中,它首先要求僱員的數量。 if循環說,如果輸入是輸入< 1輸出說「請輸入一個值至少1」。如果輸入0或-1,則輸出供用戶糾正,但只能執行一次。所以如果用戶輸入-1兩次,它將會第二次運行。它也用第二個if循環來做到這一點。C++ 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;
}
有沒有這樣的事情,如果循環。 – chris 2013-03-25 22:02:01
一個'if'不是一個循環。 – 2013-03-25 22:02:31
是的,答案在這個問題上! – 2013-03-25 22:03:24