试图让 for 循环在同一输出上执行 5 次

Trying to get a for loop to execute 5 times on the same output

美好的一天

我编写了一个代码,可以为员工输出工资单。

此外,尽管做了很多研究(我试图自己弄清楚),但我不确定如何让我的 for 循环允许我在同一个输出屏幕上连续输入 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;
}

您可以将程序的标准输入视为连续的字符流。

例如,我的标准输入将包含以下文本:

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

请注意,在行尾会有一个行尾字符(EOL 或 C++ 中的 \n)。

第一次调用 std::getline() 将 return 名字 Alice 并将在 EOL 停止,不包括在输出中。一切顺利。

下一次调用 cin >> theHoursWorked 会将 2 读入变量,一切正常。但它不会消耗EOL,因为它不是数字的一部分。

下一次调用 cin >> thePayRate 将跳过 EOL,因为它不是数字,它将读取 3。它也不会消耗下一个 EOL。

但是,下一次调用 std::getline() 将找到一个 EOL 字符,就像第一个字符一样,它将 return 一个空字符串。

下一次调用 cin >> theHoursWorked 会从 Bob 中找到 B 并且会严重失败。从现在开始,您将不会获得预期的输入。

解决方案是在需要时正确跳过 EOL 字符和任何其他空格。有几种方法可以做到这一点。

  1. cin >> theHoursWorked 之后用虚拟 string 变量调用 std::getline()
  2. 调用 cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 跳过剩余字符直到 EOL,包括`。
  3. 使用 std::getline()cin 读取所有数据,然后在第二次调用中将 string 转换为 doublegetline(cin, line); std::istringstream(line) >> theHoursWorked;.

我可以看到 2 个解决方案: 添加一个 cin.ignore();到 getData 函数末尾的行应该可以解决问题。 要么 将 getline 更改为 getline(cin>>std::ws, theEmployee);

cin 默认跳过所有空格(空格、制表符、换行符等),因此在多行 cin 程序中,如果用户在前一个 cin 命令中按 'enter',则忽略该换行符并且 cin 将正确提取您想要的数据。

第一次循环getData后,在标准输入缓冲区中留下了一个换行符'\n'(来自用户输入的PayRate信息)。但是,getline 命令不会跳过输入缓冲区中留下的这个换行符。事实上,当它看到一个换行符(因此是 getline)时,它会立即终止,返回一个空字符串。要强制 getline 命令忽略此空格,请使用 getline(cin>>std::ws, ...)。或者,您可以使用 cin.ignore() 来处理留在标准输入缓冲区中的这个换行符。