首页 > 解决方案 > 有 cin 功能时检查用户连接

问题描述

嗨,今天我试图在有 cin 时不断检查用户的连接性。

这是代码:

while (true)
{
    if (InternetCheckConnection(networkBuffer, FLAG_ICC_FORCE_CONNECTION, 0) != false)
    {
        while (cin >> userInput)
        {
            cout << "> " << userInput << endl;
        }
    } else {
        break;
    }
}

我确定问题出在 cin 上,但我必须不断检查用户的连接性(同时还有 cin 正在进行中)。

. . .

有些人在这里(在stackoverflow上)告诉我使用线程......只是我不知道如何使用它们来解决这个问题。所以我尝试了这个方法

void test()
{
    if (InternetCheckConnection(networkBuffer, FLAG_ICC_FORCE_CONNECTION, 0) != false
    {
        cout << "success" << endl;
    } else {
        exit(0);
    }
}

int main()
{
    thread first;
    cout << "1: " << first.joinable() << endl;

    first = thread(test);
    cout << "2: " << first.joinable() << endl;

    string s;
    cin >> s;

    first.join();
    cout << "3: " << first.joinable() << endl;

    system("PAUSE");
}

但是这段代码的唯一问题是他在 cin 之前检查,而不是在 cin 存在时检查。

谢谢。

标签: c++

解决方案


推荐阅读