0

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;
}
3
  • 6
    There's no such thing as an if loop. Commented Mar 25, 2013 at 22:02
  • An if is not a loop. Commented Mar 25, 2013 at 22:02
  • 1
    Yep, the answer is in the question! Commented Mar 25, 2013 at 22:03

1 Answer 1

2

Change this:

if(employeeNum < 1)

to this:

while(employeeNum < 1)

You want looping. And as chris mentions, if does no looping.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks i feel stupid, i have no idea why i did that thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.