2017-04-09 25 views
1

美好的一天試圖獲得一個for循環執行5次相同的輸出

我已經寫了一個代碼,爲員工輸出薪資單。

此外,儘管做了大量的研究(我試圖弄清楚我自己),我不知道如何讓我的循環,讓我連續輸入5個不同的員工在同一輸出屏幕上的信息。當我運行該程序時,它允許我輸入工資單的所有信息,除了每個新工資單開始時的員工姓名。

我是一個初學者想盡可能多地學習,所以任何解釋將不勝感激。

我的代碼如下:

#include <iostream> 
#include <string> 

using namespace std; 

void getData (string & theEmployee , float & theHoursWorked, float & 
thePayRate) 
{ 
    cout<< "Enter the employees name and surname: "<< endl; 
    getline(cin, theEmployee); 

    cout << "Enter the numbers of hours the employee worked: " << endl; 
    cin >> theHoursWorked; 

    cout << "Enter the employees hourly pay rate?" << endl; 
    cin >> thePayRate; 

} 

    float calculatePay(const string & theEmployee, float theHoursWorked, float 

    thePayRate) 
{ 
    float regularPay, thePay, overtimeHours; 
    if (theHoursWorked > 40) 
{ 
regularPay = 40 * thePayRate; 
overtimeHours = theHoursWorked - 40; 
thePay = regularPay + (overtimeHours * 1.5 * thePayRate); 
return thePay 
} 
else 
thePay = theHoursWorked * thePayRate; 
return thePay; 
} 

void printPaySlip(const string & theEmployee, float theHoursWorked, float 
thePayRate, float thePay) 
{ 
float overtimeHours; 
cout << "Pay slip for " << theEmployee <<endl; 
cout << "Hours worked: "<< theHoursWorked << endl; 
if (theHoursWorked > 40) 
overtimeHours = theHoursWorked - 40; 
else 
overtimeHours = 0; 
cout << "Overtime hours: "<< overtimeHours << endl; 
cout << "Hourly pay rate: " << thePayRate << endl; 
cout << "Pay: " << thePay << endl; 
cout << endl; 

} 


int main() 
{ 
string theEmployee; 
float theHoursWorked; 
float thePayRate; 
int thePay; 

for (int i = 0; i < 5; i++) 
{ 
    getData(theEmployee, theHoursWorked, thePayRate); 
    thePay = calculatePay (theEmployee, theHoursWorked, thePayRate); 
    printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay); 
} 

return 0; 
} 
+2

混合getline和cin >> int,請參閱http://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin或簡寫形式,嘗試把它放在getData的末尾:cin.ignore( std :: numeric_limits :: max(),'\ n'); –

+0

如果我使用其他兩個下面的getline它說,有一個錯誤 – Geo

+0

我將如何正確地聲明,但? – Geo

回答

0

你可以看到你的程序的標準輸入作爲字符的連續流。

例如,我的標準輸入將包含以下文本:

Alice\n 
2\n 
3\n 
Bob\n 
3\n 
2\n 
Charlie\n 
1\n 
1\n 

注意,在一行的末尾會有結束線的字符(EOL或\n在C++)。

第一次撥打std::getline()將返回第一個名字Alice,並停止在EOL,不包括在輸出中。一切都很好。

下一次撥打cin >> theHoursWorked會將2讀入變量,一切都很好。但它不會消耗EOL,因爲它不是數字的一部分。

下一次撥打cin >> thePayRate將跳過EOL,因爲它不是數字,它會讀取3。它也不會消耗下一個EOL。

但是,接下來撥打std::getline()會發現一個EOL字符就像第一個字符一樣,它會返回一個空字符串。

cin >> theHoursWorked的下一個呼叫將會從Bob找到B,並且它會失敗。從現在開始,你不會得到預期的輸入。

解決方案是在需要時正確跳過EOL字符和任何其他空格。有幾種方法可以做到這一點。

  1. 呼叫std::getline()只有cin >> theHoursWorked後一個虛擬string變量。
  2. 致電cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');跳過剩餘的字符直到EOL,包括`。
  3. 使用std::getline()讀取cin中的所有數據,然後在第二次調用getline(cin, line); std::istringstream(line) >> theHoursWorked;時將string轉換爲double
+0

好的,謝謝。我開始明白了。我怎麼會聲明std :: getline()函數呢?並且我可以問一個虛擬字符串是什麼意思嗎? – Geo

+0

@Geo:只要做'字符串虛擬; getline(cin,dummy);'完成了。我把這個變量稱爲虛擬變量,因爲它的值在被分配後沒有被使用。 – rodrigo

+0

非常感謝你,我的代碼現在可以正常工作。感謝您花時間向我解釋! – Geo

0

有兩種解決方案我可以看到: 添加一個cin.ignore();行到getData函數的結尾應該解決問題。 或 將getline改爲getline(cin >> std :: ws,theEmployee);如果用戶在前一個cin命令中按下'enter',那個新行將被忽略,並且cin將被忽略,並且cin將被忽略將正確提取你想要的數據。

getData的第一個循環後,標準輸入緩衝區(從用戶輸入PayRate的信息)中保留一個換行符'\ n'。但是,getline命令不會跳過輸入緩衝區中留下的這一換行符。實際上,它在看到換行符(因此getline)時會立即終止,並返回一個空字符串。要強制getline命令忽略這個空格,使用getline(cin >> std :: ws,...)。或者,您可以使用cin.ignore()來處理標準輸入緩衝區中留下的換行符。