user1807815
user1807815

Reputation: 83

c++ functions while loop

When I type in a decimal for number of employees it outputs "Please enter a value of atleast 1" an infinite number of times. But shouldnt the decimal be truncated? How do i fix this so the decimal is erased. This happens for the input of daysMissed also.

#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;

}

Upvotes: 0

Views: 160

Answers (2)

Serdalis
Serdalis

Reputation: 10489

After each un successful cin operation you need to use:

cin.clear();

to reset the failure flag.
and then use:

cin.ignore();

to ignore the string that just caused the error so you don't attempt to parse it again.

You should be doing something like:

int employeeNum = 0;
cout << "Enter the number of company employees: ";

while (!(cin >> employeeNum) || employeeNum < 1)
{
    if(cin.fail()) 
    {
        cout << endl << "Please enter a valid value: ";
        cin.clear ();   // reset the "failure" flag
    } 
    else if(employeeNum < 1) 
    {
        cout << "Please enter a value of at least 1: ";
    }
    // ignore the bad charactors
    // needed here incase '0.XX was entered
    cin.ignore(10000, '\n');
}

If the value you enter is a decimal then the decimal will be truncated, not rounded.

The default for ignore is to only skip over 1 character, but if you expect to get a long invalid string (like a small decimal: 0.23), you should change the ignore to:

cin.ignore(10000, '\n') 

Which will skip up to 10000 characters or until the next newline.

Upvotes: 2

Tony Delroy
Tony Delroy

Reputation: 106096

int employeeNum;
cin >> employeeNum;

This code attempts to parse an int from std::cin. Firstly, you're not actually checking the stream state afterwards - you assume that employeeNum < 1 sufficiently communicates failure, but that's not normally the best approach: there could be an EOF or error condition on the input stream. For the former your program might need to terminate, for the latter you probably want to ignore the rest of that input line and clear the error condition before retrying:

#include <limits>

if (std::cin >> employeeNum)
    break; // no need to retry input...
else if (std::cin.eof())
    throw std::runtime_error("EOF on input");
else
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n').clear();

But shouldnt the decimal be truncated? How do i fix this so the decimal is erased.

If you want to input a real number then use its "truncated" value, you need to input to a double or float then convert it to an int in code. Assuming you don't need range checking...

double x;
if (std::cin >> x)
{
    int x_int = x;
    ...use x_int...
}

Upvotes: 2

Related Questions