C++ 什么是cin.peek,它有什么作用? #包括 使用名称空间std; int main() { //声明数据类型 int NumChild;//var const int BaseFee=150;//常数 //用户欢迎+说明 cout
。该代码正在检查以确保流中的下一个字符是文件的结尾或行的结尾,而不会从流中提取字符并破坏后续的有效输入 它不能接受浮点数,因为C++ 什么是cin.peek,它有什么作用? #包括 使用名称空间std; int main() { //声明数据类型 int NumChild;//var const int BaseFee=150;//常数 //用户欢迎+说明 cout,c++,iostream,cin,C++,Iostream,Cin,。该代码正在检查以确保流中的下一个字符是文件的结尾或行的结尾,而不会从流中提取字符并破坏后续的有效输入 它不能接受浮点数,因为cin>>NumChild将读取int,并在到达小数点后立即停止。例如:输入“3.14”。NumChild将包含3.“.14”保留在流中,因此peek将读取。,而不是文件的结尾或换行符,并打印错误消息 然后它继续执行程序的其余部分,因为没有任何命令它停止。在提供有效输入之前,需要围绕输入进行循环以继续请求更多输入 一个简单的例子: #include <iostrea
cin>>NumChild
将读取int
,并在到达小数点后立即停止。例如:输入“3.14”。NumChild
将包含3.“.14”保留在流中,因此peek将读取。
,而不是文件的结尾或换行符,并打印错误消息
然后它继续执行程序的其余部分,因为没有任何命令它停止。在提供有效输入之前,需要围绕输入进行循环以继续请求更多输入
一个简单的例子:
#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>>NumChild
将读取int
,并在到达小数点后立即停止。例如:输入“3.14”。NumChild
将包含3.“.14”保留在流中,因此peek将读取。
,而不是文件的结尾或换行符,并打印错误消息
然后它继续执行程序的其余部分,因为没有任何命令它停止。在提供有效输入之前,需要围绕输入进行循环以继续请求更多输入
一个简单的例子:
#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;
}
}
输入流上的“peek”函数(在您的例子中是
cin
)从流中检索下一个字符,而不实际使用它。这意味着您可以“预览”输入中的下一个字符,并在下次调用任何使用操作时(重载运算符>
或cin.read
)将读取该字符并使用它
条件eof()
关于您的另一个问题:如果输入无效(例如浮点值),您不会退出该函数。因此,您将继续执行并打印该值。只需使用输入流上的“peek”函数退出该函数即可(在您的情况下,cin
)从流中检索下一个字符,而不实际使用它。这意味着您可以“预览”输入中的下一个字符,并且在下一次调用任何使用操作时(重载操作符>
或cin.read
)将读取该字符并使用它
条件eof()
关于您的另一个问题:如果输入无效(例如浮点值),您不会退出函数。因此,您将继续执行并打印值。只需使用return 1;
退出函数即可