2013-03-26 80 views
0

當我爲員工數輸入一個小數時,它輸出「請輸入一個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; 

} 

回答

2

每一未成功cin操作後,您需要使用:

cin.clear(); 

復位故障標誌。
然後用:

cin.ignore(); 

忽略,只是導致了錯誤,所以你不要試圖再次解析它的字符串。

你應該做這樣的事情:

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'); 
} 

如果你輸入的值是一個小數,則小數將被截斷,而不是四捨五入。

ignore默認是隻跳過1個字符,但如果你希望得到一個長期無效的字符串(如小十進制:0.23),你應該改變ignore到:

cin.ignore(10000, '\n') 

哪將跳過最多10000個字符或直到下一個換行符。

+0

您可能還想擺脫冒犯的字符或行。 – chris 2013-03-26 01:55:33

+0

@chris相當真實!忘了忽略。 – Serdalis 2013-03-26 01:59:20

+0

它回答這個問題:「但不應該小數點被截斷?」 – 4pie0 2013-03-26 02:06:44

2
int employeeNum; 
cin >> employeeNum; 

此代碼嘗試解析從std::cin一個int。首先,事後並未真正檢查數據流狀態 - 您認爲employeeNum < 1可以充分傳達故障,但這通常不是最佳方法:輸入數據流中可能存在EOF或錯誤條件。對於前者程序可能需要終止,後者則可能要忽略輸入行的休息和試之前清除錯誤條件:

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

但不應該小數被截斷?我如何解決這個問題,所以小數點被刪除。

如果你想輸入則實數使用其「截斷」值,則需要輸入到doublefloat然後將其轉換成int代碼。假設您不需要範圍檢查...

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