此问题在此处已有答案:
Why does std::getline() skip input after a formatted extraction?(5个答案)
9天前关闭
由于某种原因,第二次循环时,cout语句会重叠。换句话说,在第一次cout之后,程序不会等待输入。我该如何解决这个问题?
另外,在真实的生活税的情况下,nPay函数工作正常吗?有人告诉我,税应该乘以总收入,每个单独的,并添加。然而,我的方法将工作相同,特别是因为它们同时发生。
double calcGrossPay (double payRate, double hours);
double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay);
void displayAll (double fPay, double netPay, string name);
double fedTx = 14, stateTx = 6, localTx = 3.5, ssTx = 4.75;
int main()
{
while (!cin.eof())
{
string name;
//cin.ignore();
cout <<"Please enter your working name: ";
getline (cin, name);
!cin.eof();
double payRate, hours;
cout <<"Enter your pay rate and hours worked, respectively."<< endl;
cin >> payRate >> hours;
!cin.eof();
double fPay = calcGrossPay (payRate, hours);
double netPay = 0;
netPay = nPay (fedTx, localTx, stateTx, ssTx, netPay, fPay);
displayAll (fPay, netPay, name);
system("pause");
}
}
double calcGrossPay (double payRate, double hours)
{
double extraT, fPay;
if (hours > 40)
{
extraT = (hours - 40) * (1.5 * payRate);
fPay = extraT + (40 * payRate);
}
else
fPay = payRate * hours;
return fPay;
}
double nPay (double& fedTx, double& localTx, double& stateTx, double& ssTx, double& netPay, double fPay)
{
double totalTx = fedTx + localTx + stateTx + ssTx;
netPay = fPay * (1 - (totalTx / 100));
return netPay;
}
void displayAll (double fPay, double netPay, string name)
{
cout <<"Below is "<< name << "'s salary information" << endl;
cout << fixed << showpoint << setprecision(2) <<"\nYour calculated gross pay is $"
<< fPay << ", and your net pay is $" << netPay << endl;
}
字符串
2条答案
按热度按时间gdx19jrr1#
在
getline
之后,一个新的行仍然在流中,所以你必须ignore
它:字符串
此外,在检查流之前执行提取,而不是
while (!cin.eof())
:型
下面是更新后的代码。我希望它对你有用:
型
7kqas0il2#
在Windows和MacOS中,在一行的末尾有一个回车字符(/r),当编译器读取\r时,它会将光标返回到该行的开头,如果你想避免这种情况发生,你可以通过阅读整个文件并在每次遇到它时将其设置为null(\0)来消除该字符。