重叠 cout [复制]

Overlapping cout [duplicate]

提问人:matttm 提问时间:12/5/2013 更新时间:11/9/2023 访问量:853

问:

出于某种原因,第二次迭代此循环时,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;
}
C++ 循环 EOF

评论

0赞 chris 12/5/2013
而 (!eof()) 是错误的。

答:

2赞 David G 12/5/2013 #1

之后,流中仍有一条新行,因此您必须这样做:getlineignore

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();
    }
}

评论

0赞 matttm 12/5/2013
忽略工作,但我不明白 eof 去哪儿了。我以为它会在每个 cin 之后进行,因为当用户输入 ctrl-z 时,prog 会退出
0赞 David G 12/6/2013
@matttm 您不需要 .给我一个给程序输入的例子,我会告诉你该怎么做。!eof()
0赞 matttm 12/6/2013
代码 cout <<“请输入您的工作名称:”;getline (CIN, 名称);cin.ignore();cin.eof();法典当输入 ctrlZ 时,eof 就是这样,prog 结束。
0赞 David G 12/6/2013
@matttm 请检查更新并告诉我它是否适合您。
0赞 David G 12/6/2013
@matttm哎呀!我在原始代码中犯了一个错误。不过我刚才修好了。:D
-1赞 Riccardo Romeo 11/9/2023 #2

在 Windows 和 MacOs 中,在一行的末尾有一个回车符 (/r),当编译器读取 \r 时,它会将光标返回到行的开头,如果你想避免这种情况发生,你可以通过读取整个文件并将其设置为 null(\0) 来消除该字符每次遇到它。