【问题标题】:Trying to get a for loop to execute 5 times on the same output试图让 for 循环在同一输出上执行 5 次
【发布时间】:2017-04-09 19:47:07
【问题描述】:

美好的一天

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

此外,尽管进行了大量研究(我试图自己弄清楚),但我不确定如何让我的 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;
}

【问题讨论】:

  • 混合 getline 和 cin >> int,参见 stackoverflow.com/questions/5739937/… 或缩写形式,尝试将其放在 getData 的末尾: cin.ignore(std::numeric_limits<:streamsize>::max (), '\n');
  • 如果我在其他两个下使用 getline 则表示有错误
  • 我该如何正确声明呢?

标签: c++


【解决方案1】:

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

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

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

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

std::getline() 的第一次调用将返回名字Alice 并将在EOL 处停止,不包括在输出中。一切都很好。

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

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

但是,下一次对std::getline() 的调用会发现一个EOL 字符与第一个字符一样,它会返回一个空字符串。

cin &gt;&gt; theHoursWorked 的下一次调用将从Bob 中找到B,它将严重失败。从现在开始,您将无法获得预期的输入。

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

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

【讨论】:

  • 好的,谢谢。我开始明白了。我将如何声明 std::getline() 函数?我能问一下你所说的虚拟字符串是什么意思吗?
  • @Geo:只需执行string dummy; getline(cin, dummy); 即可。我称该变量为 dummy 是因为它的值在分配后未被使用。
  • 非常感谢,我的代码现在可以正常工作了。感谢您花时间向我解释!
  • @Geo:没问题,这就是 SO 的用途。如果您认为答案还可以,请不要忘记接受和/或投票。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多