C++ C++;如果(!cin)导致循环

C++ C++;如果(!cin)导致循环,c++,cin,C++,Cin,我尝试使用if(!cin)来验证用户输入是否真的是整数。然而,我的程序只是进入一个无限循环,从不要求新的输入 do{ cin >> temp->data; if(!cin){ cout << "Please enter a Number!" << '\n'; correct=false; } }while(correct==false); do{ cin>>温度->数据; 如果(!c

我尝试使用if(!cin)来验证用户输入是否真的是整数。然而,我的程序只是进入一个无限循环,从不要求新的输入

do{
    cin >> temp->data;
    if(!cin){
        cout << "Please enter a Number!" << '\n';
        correct=false;
        }
   }while(correct==false);
do{
cin>>温度->数据;
如果(!cin){
不能使用
cin.fail()
检查用户输入的输入是否正确。
cin.fail()
如果最后一个
cin
命令失败,则返回
true
,否则返回
false
。此外,您的循环可能是无限的,因此您还必须声明一个
else
,您将在其中将检查标志
correct
设置为
true
。这样,可以使循环的条件无效并在cas中退出循环e用户输入了正确的输入(见下面的代码):

do{
cin>>温度->数据;
if(cin.fail()){
cin.clear();
cin.忽略(10000,“\n”);

cout如果要执行此类检查,请将
cin
中的数据读取为
字符串
,并将
字符串
转换为数字:

string str;
do{
    cin >> str;
    if(!cin){
        cout << "Please enter a Number!" << '\n';
        correct=false;
        }
    else{
        istringstream stream(str);
        stream >> temp->data;
        if(!stream){
            cout << "Please enter a Number!" << '\n';
            correct=false;
        }
     }

   }while(correct==false);
string-str;
做{
cin>>str;
如果(!cin){
cout temp->data;
如果(!流){

cout当std::cin无法读取输入时,会设置相应的错误标志。因此,您希望使用std::cin.clear()重置标志,以便下一个输入操作正常工作,然后跳过所有操作,直到使用std::cin.ignore(..)的新行出现,以避免类似格式的输入

while (!(std::cin >> temp->data))
{
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "\nPlease enter a number!" << std::endl;
}
while(!(标准:cin>>温度->数据))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(),'\n');

std::cout您的“correct”变量实际上没有您使用它的方式做任何事情。如果
correct
为true,则不可能退出循环;因此您可以取消它,只需在读取数字后使用循环退出命令即可

此外,到目前为止发布的答案中没有一个能够处理关闭的输入。在这种情况下,它们将进入无限循环

// A loop; we will break out when we successfully read a number.
while ( 1 )
{
// Prompt for a number and read it
    cout << "Please enter a Number!" << endl;
    cin >> temp->data;

// Exit loop if we successfully read
    if ( cin )
         break;

// Check to see if we failed due to the input being closed
    if ( cin.eof() )
    {
        cerr << "End of input reached.\n";
        return 0;   // depends what your function returns of course
    }

// reset the error condition that was caused by trying to read an integer and failing
    cin.clear();

// discard anything they previously typed
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

它将输出
编号
,并通过抛出异常,或依靠调用方检查
!cin
,甚至返回
bool
,来处理文件结尾。如果你在错误处理中加上“
”,那么它将处理文件结尾在
if
语句中的条件之前。这应该是一个“not”运算符。

您需要清除流的错误条件。但是最好将读取行和解析行的任务分开。阅读此命令将
correct=false
移动到
do
之前的声明/初始化中,同时进行测试
}(!正确)
当你在没有任何解释的情况下粘贴正确的代码时,OP将如何从中学习到任何东西?这是一种非常混乱的输入方式。@Veritas Verum quidem dicis@Veritas,正如你的名字所说,你说的是实话。然而,OP并没有要求最优雅、最有效的输入方式。他问自己为什么上厕所我们的解决方案非常接近,但我不确定seelenkuchen是否真的打算跳过第一个提示…“请输入一个数字!”如果用户没有按预期输入一个数字,感觉就像是一条错误消息。否则,他有很多方法可以处理它,要么在循环外编写另一个std::cout,要么使用逻辑运算符,要么在答案中使用类似于break的符码。我想,您的运算符优先级在测试中被破坏。
之前,right?你说得对。事实上我只是在双重检查运算符优先级。我刚刚注意到“!”在他的消息中,假设没有第一个提示,则删除我的答案而代之以您的答案。忽略成员函数在eof处停止,而不管指定的字符是什么。@Veritas one希望如此。这与我发布的内容有关吗?只是说我不理解您在当不涉及输入间接寻址时,eof。当
cin.eof()
发生时,您的post和4020的post都会陷入无限循环中。退出循环的唯一方法是成功读取数字,而在eof之后则不会发生。不,不会,至少在我的机器上不会。您是如何进入eof的?
// A loop; we will break out when we successfully read a number.
while ( 1 )
{
// Prompt for a number and read it
    cout << "Please enter a Number!" << endl;
    cin >> temp->data;

// Exit loop if we successfully read
    if ( cin )
         break;

// Check to see if we failed due to the input being closed
    if ( cin.eof() )
    {
        cerr << "End of input reached.\n";
        return 0;   // depends what your function returns of course
    }

// reset the error condition that was caused by trying to read an integer and failing
    cin.clear();

// discard anything they previously typed
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
void input_number(int &the_number, std::istream &in, std::string prompt);