Overlapping cout
Overlapping cout
本文关键字:cout Overlapping 更新时间:2023-10-16
由于某种原因,第二次迭代此循环时,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;
}
在getline
之后,一个新行仍在流中,因此您必须ignore
it:
getline(cin, name);
cin.ignore();
此外,代替while (!cin.eof())
,在检查流之前进行提取:
while (getline(cin, name))
{
cin.ignore();
// ...
}
这是更新后的代码。我希望它对你有效:
int main()
{
for (std::string name; (cout << "Please enter your working name: ") &&
getline(cin >> std::ws, name);)
{
if (cin.eof())
break;
double payRate, hours;
cout << "nEnter your pay rate and hours worked, respectively." << endl;
if (!(cin >> payRate >> hours))
break;
double fPay = calcGrossPay(payRate, hours);
double netPay = nPay(fedTx, localTx, stateTx, ssTx, netPay, fPay);
displayAll(fPay, netPay, name);
cin.get();
}
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 在 COUT 语句中使用 COUT 调用函数
- GCC 4.8.2 自动矢量化由于 cout 而失败
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- C++ Setter/Getter,cout 工作,printf 失败
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 结构元素名称要 cout?
- 使用模板类重载 cout
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- 带有 cout 的指针在C++
- cout 指针时的实际数字
- COUT 无法提供输出可能是由于内联组装
- C++程序在一定行后未正确编译[cout语句]
- Overlapping cout