std :: getline(std :: cin)设置failbit

时间:2014-10-23 15:13:53

标签: c++ c++11

我不知道发生了什么,我用cin尝试了各种各样的事情,包括忽略有和没有参数和同步以及各种各样的东西。我所要做的就是getline(cin, str)。如果这对任何事都有帮助的话,我会参加2013年的visual studio express。

这是我的代码:

bool prompt(std::string &str)
{
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl;
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl;
    pause();
    return true;
}

修改:删除了cin.ignorecin.sync。错误仍然发生

截至目前,该程序输出“一切正常”和“cin失败”。除了“cin失败”之后没有暂停。

有什么想法吗?非常感谢你。

5 个答案:

答案 0 :(得分:3)

我使用Visual Studio Ultimate 2013 Update 3编译了以下代码.hause();声明已被注释掉,因为我不知道它是什么。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

bool prompt(std::string &str)
{
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl;
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl;
    // pause();
    return true;
}

int main()
{
    string strIn;
    prompt(strIn);
    cout << "Input: " << strIn << endl;
}

运行时,输出为

>
Everything is ok
Input Test.
Input: Input Test.

我没有看到failbit问题。也许Visual Studio Express 2013出了问题?

来自http://www.cplusplus.com/reference/string/string/getline/

  

failbit:获取的输入无法解释为有效的文本   表示此类对象。在这种情况下,分发   保留调用前的参数和内部数据。   请注意,某些eofbit案例也会设置failbit。

如果输入Ctrl + Z作为输入,&#34; cin失败&#34;确实出现了:

>
Everything is ok
^Z
cin is failed
Input:

答案 1 :(得分:0)

确实

if(cin) 
  std::cout "everything is okay";
如果cin设置了failbad位,则

不会打印该语句,但如果它具有eof bit set,则不会打印该语句:

bool prompt(std::string &str)
{
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl; // Will be printed
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl; // Will be printed
    return true;
}
int main()
{
    std::string obj;
    std::cin.setstate(std::ios::eofbit); // Simulate an eof/Ctrl^Z on Win read
    prompt(obj);
}

Reproducer

之前的操作可能已将其设置,这也在one of your previous topics中进行了讨论。

为确保cin处于有效状态,您应首先使用cin.clear();

取消设置其状态位
bool prompt(std::string &str)
{
    cin.clear(); // Reset state bits
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl;
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl;
    return true;
}

如果您的代码仍然失败,请确保您实际上正在阅读有效的文字输入并避免阅读invalid characters

答案 2 :(得分:0)

我在eclipse上重现了这个问题:

#include <iostream>
#include <string>


using namespace std;

bool prompt(std::string &str)
{
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl;
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl;
    return true;
}

int main()
{
    string strIn;
    prompt(strIn);
    cout << "Input: " << strIn << endl;
}

现在您将获得输出:

> 
Everything is ok
cin is failed
Input:

每当你在eclipse上终止正在运行的程序时。这意味着其他一些进程可能正在终止正在运行的程序。

答案 3 :(得分:0)

我能够通过几种不同的方法来模仿这些结果,所有这些方法都涉及一些重新定义cin的奇怪事物或者具体地将cin设置为奇怪状态。以下代码将生成此问题:

#include <string>
#include <iostream>
using namespace std;

bool prompt(std::string &str)
{
    cout << "> ";
    if (cin) cout << endl << "Everything is ok" << endl;
    std::getline(std::cin, str);
    if (!cin) cout << "cin is failed" << endl;
    return true;
}

int main()
{
    cin.setstate(ios::eofbit);
    string test;
    prompt(test);
}

该代码生成输出:

>
Everything is ok
cin is failed

此代码为cin设置eof标志,这不会导致它失败,因此对cin的检查返回true,但任何立即读取它的尝试都会失败。另一种方法涉及改变cin的定义(或者在我的情况下,我复制它然后修改它并包括副本)。这些是我能够计算出你的功能在visual studio 2013中产生的输出的唯一方法。

答案 4 :(得分:0)

我发现了发生了什么事。令人难以置信的愚蠢,我完全忘记了我是如何分配控制台的。在我的主要功能中:

if (AllocConsole())
{
    freopen("CONOUT$", "wt", stdout);
    ShowWindow(GetConsoleWindow(), SW_SHOW);
}

我使用这个片段永远在互联网上漂浮。但是,我没有想到输入流还没有链接到控制台。我添加了这一行:

freopen("CONIN$", "rt", stdin);

这似乎就是诀窍。