简体   繁体   English

带有 std::cin >> char 的 C++ 调试断言错误

[英]C++ debug assertion error with std::cin >> char

Background : The problem/code shown here is part of a larger program I am trying to create (I was told to trim it down).背景:此处显示的问题/代码是我尝试创建的较大程序的一部分(有人告诉我将其修剪掉)。 The purpose (of this section) is to be able to receive std::cin input and (when it works) feed it into the function: srcdsControl->WriteText(string(chr)); (本节的目的)是能够接收 std::cin 输入并(当它工作时)将其输入到函数中: srcdsControl->WriteText(string(chr)); This function is part of the larger program, which I have not created myself.这个函数是更大程序的一部分,它不是我自己创建的。

I'm trying to redirect input and output from a C++ console application to a Python script (which I have written, and which works).我正在尝试将输入和输出从 C++ 控制台应用程序重定向到 Python 脚本(我已经编写并且可以运行)。 When I type into the console window, it receives the input fine.当我在控制台窗口中输入时,它会很好地接收输入。 However, when I send input to the program's stdin from python (in the format 'command\\r\\n'), it triggers the following error:但是,当我从 python 向程序的 stdin 发送输入(格式为“command\\r\\n”)时,它会触发以下错误:

C++ 调试断言失败错误

I've tried with both of the following programs and they give exactly the same error.我已经尝试过以下两个程序,它们给出了完全相同的错误。

My C++ code (test 1):我的 C++ 代码(测试 1):

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

int main()
{
    try
    {
        char chr[2];
        chr[1] = 0;
        while (1)
        {
            if (std::cin >> std::noskipws >> chr[0])
            {
                if (chr[0] == '\r')
                    std::cout << "\r";
                //srcdsControl->WriteText(string(chr));
                std::cout << std::string(chr) << "\r\n";
            }
        }
    }
    catch (...)
    {
        // Ignore exception and just stop the thread.
    }
}

My C++ code (test 2):我的 C++ 代码(测试 2):

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

int main()
{
    try
    {
        char chr[2];
        chr[1] = 0;
        while (1)
        {
            std::string s = "";
            if (std::getline(std::cin, s))
            {
                for (char& ch : s) {
                    chr[0] = ch;
                    if (chr[0] == '\r')
                        std::cout << "\r";
                    //srcdsControl->WriteText(std::string(chr));
                    std::cout << std::string(chr) << "\r\n";
                }
            }
        }
    }
    catch (...)
    {
        // Ignore exception and just stop the thread.
    }
}

Note : I am not a C++ developer and my knowledge of C++ is very limited.注意:我不是 C++ 开发人员,我对 C++ 的了解非常有限。 I need a specific/simple answer.我需要一个具体/简单的答案。

My question : How do I prevent this error and ensure that the input is properly received?我的问题:如何防止此错误并确保正确接收输入? I am happy to use a completely different method to receive the input if needed, but would need a specific demonstration of it.如果需要,我很高兴使用完全不同的方法来接收输入,但需要对其进行具体演示。

I somehow managed to fix my own problem.我以某种方式设法解决了我自己的问题。

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

int main()
{
    try
    {
        char chr[2];
        chr[1] = 0;
        while (1)
        {
            char c;
            if (std::cin >> std::noskipws >> c)
            {
                chr[0] = c;
                if (chr[0] == '\r')
                    std::cout << "\r";
                //srcdsControl->WriteText(string(chr));
                std::cout << std::string(chr) << std::endl;
            }
        }
    }
    catch (...)
    {
        // Ignore exception and just stop the thread.
    }
}

This code seems to do what I want without giving the error.这段代码似乎做了我想要的,而没有给出错误。 If anyone can explain why this works and the previous code didn't that would be interesting to know.如果有人能解释为什么这行得通,而之前的代码却没有,那会很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM