【问题标题】:What is cin.peek, and what does it do?什么是cin.peek,它有什么作用?
【发布时间】:2019-04-26 08:26:56
【问题描述】:
#include <iostream>
using namespace std;
int main() 
{
//Declare data types
int NumChild; //var
const int BaseFee = 150; //constant

//Welcome + Instructions for users
cout << "Welcome to IOU Secondary School" << endl <<endl<< "How many 
children are attending the school this year? "<<endl;

//Nested IF-ELSE (error checking)
if (cin >> NumChild)/

    {

        //do not accept any non-integer values
        if (cin.peek() == EOF|| cin.peek() == '\n' )
        {

        }
        else
        { 

            cout <<endl<< "Error: enter a whole number."<<endl<< "The total 
   amount below represents the initial number entered e.g. if 5.7 is entered, the fee will be calculated according to 5 children" << endl;//error message for float entry
            cin.ignore(100, '\n'); //read and discard up to 100 characters from the input buffer, or until a newline is read
            cin.clear();//resets any error flags in the cin stream

        }
    }
    else
    {
        // Edit: You should probably also clear the steam
        cin.ignore(100, '\n');//read and discard up to 100 characters from 
        the input buffer, or until a newline is read
        cin.clear(); //resets any error flags in the cin stream
        cout << "Error: Restart the program and enter a number";

    }

    //nested IF statement (Calculation of fees)
if(NumChild == 1) //1 child attending
{
cout<<endl<<"The total cost of your child's fees are $"<< BaseFee+50 << 
endl;
}

if(NumChild == 2) //2 children attending
  {
cout<<endl<<"The total cost of your children's fees are $"<< (BaseFee*2)+85 
<< endl;
  }

if(NumChild == 3)//3 children attending
  {
cout<<endl<<"The total cost of your children's fees are $"<< (BaseFee*3)+110 
<< endl;
  }

if(NumChild > 3) //More than children attending
  {
cout<<endl<<"The total cost of your children's fees are $"<< 
(BaseFee*NumChild)+150 << endl;
  }
}

有人可以解释一下 cin.peek 语句是什么以及在上面的代码中它在做什么吗? EOF|| cin.peek() == '\n') 对此有何影响。

当我输入一个浮点值时,第一个 else 值激活,但结果仍然显示。如何调整代码以使总费用不显示?

【问题讨论】:

    标签: c++ iostream cin


    【解决方案1】:

    输入流上的“窥视”功能(在您的情况下为cin)从流中检索下一个字符而不实际使用它。这意味着您可以“预览”输入中的下一个字符,并在下一次调用任何消费操作(重载 operator &gt;&gt;cin.read)时读取该字符并使用它。

    条件eof() || cin.peek == '\n' 检查是否到达输入文件流的末尾或用户是否提供了换行符。

    关于您的其他问题:如果输入无效(例如浮点值),您不会退出该函数。因此,您继续执行并因此打印该值。只需使用return 1; 退出函数即可。

    【讨论】:

      【解决方案2】:

      peek looks at a character without removing it from the stream。该代码正在检查以确保流中的下一个字符是文件的结尾或行的结尾,而不会从流中提取字符并损坏后续的有效输入。

      它不能接受浮点数,因为cin &gt;&gt; NumChild 将读取int,并且一旦到达小数点就会停止。示例:输入“3.14”。 numChild 将包含 3。“.14”保留在流中,因此 peek 将读取 '.',而不是文件末尾或换行符,并打印错误消息。

      然后它继续执行程序的其余部分,因为没有任何东西告诉它停止。您需要围绕输入进行循环以继续请求更多输入,直到提供有效输入为止。

      一个简单的例子:

      bool goodInput = false;
      while (!goodInput)
      {
          your code here
          if (cin.peek() == EOF|| cin.peek() == '\n' )
          {
              goodInput = true; // change made here
          } 
          your code here
      }
      

      【讨论】:

        猜你喜欢
        • 2011-01-14
        • 2016-04-29
        • 2017-03-02
        • 2012-10-04
        • 1970-01-01
        • 2017-06-20
        • 2019-12-25
        • 2010-10-02
        • 2015-02-06
        相关资源
        最近更新 更多