简体   繁体   English

该循环仅适用于第一次迭代

[英]This loop only works on the first iteration

This program works perfect on the first iteration, but on any iteration after, the second cin is skipped ans the message displays. 该程序在第一次迭代时可以完美运行,但是在之后的任何迭代中,第二个cin都会被跳过并显示消息。 Also, in the second iteration, if the name is two parts, like first and last name, then only the first will display. 同样,在第二次迭代中,如果名称是两个部分,例如名字和姓氏,则仅会显示第一个。

Any help? 有什么帮助吗?

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;

          cout <<"Please enter your working name: ";
          cin >> name;

          if (cin.eof())
             return 0;

          double payRate, hours;

          cout <<"Enter your pay rate and hours worked, respectively."<< endl;
          cin >> payRate >> hours;

          if (cin.eof())
             return 0;

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

cin >> operator reads data only upto the first whitespace character. cin >>运算符只能读取直到第一个空格字符的数据。

So, if your data-reading is cin>> name; cin>>payRate; cin >> hours 因此,如果您的数据读取为cin>> name; cin>>payRate; cin >> hours cin>> name; cin>>payRate; cin >> hours cin>> name; cin>>payRate; cin >> hours , and if you enter John Doe 12 34 , then "John" will be read as the name, and "doe" will be read as the payRate. cin>> name; cin>>payRate; cin >> hours ,如果您输入John Doe 12 34 ,则“ John”将被读取为名称,而“ doe”将被读取为payRate。 At this moment the data-reading will fail, since doe cannot be transformed into any numeric value. 此时,由于无法将doe转换为任何数值,因此数据读取将失败。

Also, at this point the cin stream will enter into an error state upon which it will cease to read any further data . 同样,在这一点上, cin流将进入error state ,在此error state它将停止读取任何其他数据 All attempts to read will simply be skipped. 所有读取尝试都将被跳过。 Therefore the reading cin>>hours will be skipped, and all reading in the next loop iteration will be also skipped. 因此,将跳过cin>>hours读数,并且还将跳过下一循环迭代中的所有读数。 And any other data-reading from cin that you have in your app. 以及您在应用程序中从cin读取的任何其他数据。

But you always can (and should!) check if there were any errors. 但是您始终可以(并且应该!)检查是否有任何错误。 You can check cin.good or cin.fail error flags so you can detect if the stream has entered into error state. 您可以检查cin.goodcin.fail错误标志,以便检测流是否已进入错误状态。 Search on google or StackOverflow for "cin error handling" and you will quickly find examples. 在google或StackOverflow上搜索“ cin错误处理”,您将快速找到示例。

After you detect the error, you can reset the stream by cin.clear() . 检测到错误后,可以通过cin.clear() 重置流 This does not remove any data from the stream - it just clears the error flags so that the stream starts reading the data again. 这不会从流中删除任何数据-只是清除错误标志,以便流重新开始读取数据。 Mind that the old data is NOT removed. 注意不要删除旧数据。 If after failing on cin >> payrate due to Doe you'd reset the cin, then the next data read would be doe again. 如果由于Doe而导致cin >> payrate失败后,您将重置cin,那么下一次读取的数据将再次为doe

The last part of your problem is reading the string "name" so that it can include whitespaces. 问题的最后一部分是读取字符串“名称”,以便它可以包含空格。 As I said, cin >> reads only upto the first whitespace, so it is hardlly usable here. 正如我所说, cin >>只能读取第一个空格,因此在这里几乎不可用。

Instead, switch to reading a whole line of text through std::getline function. 相反,可以通过std::getline函数切换为读取整行文本。 It's used like this: 它的用法如下:

std::getline( cin, name )

It will read whole line of text as the name, including any spaces, tabs etc. It will end reading on \\n, \\r or \\r\\n characters, depending on your OS. 它会读取整行文本作为名称,包括任何空格,制表符等。它会以\\ n,\\ r或\\ r \\ n字符结尾,具体取决于您的操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM