C++ 尝试获取一个for循环,以便在同一输出上执行5次
你好 我编写了一个代码,为员工输出工资单 此外,尽管我做了大量研究(我自己也试图弄清楚),但我不确定如何让我的for循环允许我在同一输出屏幕上连续输入5名不同员工的信息。当我运行该程序时,它允许我输入工资单的所有信息,除了每个新工资单开头的员工姓名 我是一个初学者,希望尽可能多地学习,所以任何解释都将不胜感激 我的代码如下:C++ 尝试获取一个for循环,以便在同一输出上执行5次,c++,C++,你好 我编写了一个代码,为员工输出工资单 此外,尽管我做了大量研究(我自己也试图弄清楚),但我不确定如何让我的for循环允许我在同一输出屏幕上连续输入5名不同员工的信息。当我运行该程序时,它允许我输入工资单的所有信息,除了每个新工资单开头的员工姓名 我是一个初学者,希望尽可能多地学习,所以任何解释都将不胜感激 我的代码如下: #include <iostream> #include <string> using namespace std; void getData
#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;
}
#包括
#包括
使用名称空间std;
void getData(字符串和员工、float和工时、float和
(工资率)
{
cout您可以将程序的标准输入视为连续的字符流
例如,我的stdin将包含以下文本:
Alice\n
2\n
3\n
Bob\n
3\n
2\n
Charlie\n
1\n
1\n
请注意,行尾将有一个行尾字符(EOL或C++中的\n
)
对std::getline()
的第一次调用将返回名字Alice
,并将在下线时停止,不包括在输出中。一切正常
下一次调用cin>>theHoursWorked
将把2
读入变量,一切正常。但它不会消耗下线,因为它不是数字的一部分
下一次调用cin>>支付率将跳过EOL,因为它不是一个数字,它将读取3
。它也不会消耗下一个EOL
但是,对std::getline()
的下一次调用将发现一个EOL字符,它与第一个字符一样,并返回一个空字符串
下一次调用cin>>theHoursWorked
将从Bob
中找到B
,它将严重失败。从现在起,您将无法获得预期的输入
解决方案是在需要时正确地跳过下线字符和任何其他空格
在cin>>工作时间之后,使用虚拟字符串变量调用std::getline()
调用cin.ignore(std::numeric_limits::max(),'\n');
将剩余字符跳过到EOL,包括'
使用std::getline()
读取cin
中的所有数据,然后在第二次调用中将字符串
转换为double
:getline(cin,line);std::istringstream(line)>>工作时间;
您可以将程序的标准输入视为连续的字符流
例如,我的stdin将包含以下文本:
Alice\n
2\n
3\n
Bob\n
3\n
2\n
Charlie\n
1\n
1\n
请注意,行尾将有一个行尾字符(EOL或C++中的\n
)
对std::getline()
的第一次调用将返回名字Alice
,并将在下线时停止,不包括在输出中。一切正常
下一次调用cin>>theHoursWorked
将把2
读入变量,一切正常。但它不会消耗下线,因为它不是数字的一部分
下一次调用cin>>支付率将跳过EOL,因为它不是一个数字,它将读取3
。它也不会消耗下一个EOL
但是,对std::getline()
的下一次调用将发现一个EOL字符,它与第一个字符一样,并返回一个空字符串
下一次调用cin>>theHoursWorked
将从Bob
中找到B
,它将严重失败。从现在起,您将无法获得预期的输入
解决方案是在需要时正确地跳过下线字符和任何其他空格
在cin>>工作时间之后,使用虚拟字符串变量调用std::getline()
调用cin.ignore(std::numeric_limits::max(),'\n');
将剩余字符跳过到EOL,包括'
使用std::getline()
读取cin
中的所有数据,然后在第二次调用中将字符串
转换为double
:getline(cin,line);std::istringstream(line)>>工作时间;
我可以看到两种解决方案:
在getData函数的末尾添加一个cin.ignore();行应该可以解决这个问题。
或
将getline更改为getline(cin>>std::ws,员工)
默认情况下,cin跳过所有空白(空格、制表符、新行等),因此在多cin行程序中,如果用户在以前的cin命令中按“回车”,则该新行将被忽略,cin将正确提取所需的数据
在第一次循环getData之后,标准输入缓冲区上会留下一个换行符“\n”(来自输入付款率信息的用户)。但是,getline命令不会跳过输入缓冲区上留下的这个换行符。事实上,当它看到一个换行符(因此是getline)时,它会立即终止,返回空字符串。若要强制getline命令忽略此空白,请使用getline(cin>>std::ws,…)。或者,您可以使用cin.ignore()处理标准输入缓冲区中的此换行符。我可以看到两种解决方案:
在getData函数的末尾添加一个cin.ignore();行应该可以解决这个问题。
或
将getline更改为getline(cin>>std::ws,员工)
默认情况下,cin跳过所有空白(空格、制表符、新行等),因此在多cin行程序中,如果用户在以前的cin命令中按“回车”,则该新行将被忽略,cin将正确提取所需的数据
在第一次循环getData之后,标准输入缓冲区上会留下一个换行符“\n”(来自输入付款率信息的用户)。但是,getline命令不会跳过inp上留下的这个换行符