Reputation: 93
In my program it first asks for the number of employees. The if loop says that if the input is input < 1 to output saying "Please enter a value of at least 1". If you input 0 or -1 it outputs for the user to correct it, but it only does this once. So if the user inputs -1 twice it will run the second time. It also does this with the second if loop.
#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;
}
Upvotes: 0
Views: 188
Reputation: 63937
Change this:
if(employeeNum < 1)
to this:
while(employeeNum < 1)
You want looping. And as chris mentions, if
does no looping.
Upvotes: 2