C++在检查argv值时未经手的异常

发布于 2025-02-01 05:08:57 字数 953 浏览 0 评论 0原文

有人可以告诉我为什么这个代码在10分钟前有效,但现在一直在失败?

我不断遇到异常错误。在调试菜单中,我将进入32和12.5。

每次我尝试检查I> 0;

bool CheckArgInputs(char* inputs[], int numInputs)
{
    const char validChars[] = ".,+-eE0123456789";
    bool tester = false;
    
    for (int i = 0; *inputs[i] != 0; i++)
    {
        tester = false;

        for (int j = 0; j < sizeof(validChars); j++)
        {
            if (*inputs[i] == validChars[j])
            {
                tester = true;
            }
        }
        if (tester == false)
            return false;
        //else
        //  cout << "Good Input" << endl;
    }
}

int main(int argc, char* argv[])
{
    bool validInput = true;

    for (int i = 1; i < argc; i++)
    {

        validInput = CheckArgInputs(&argv[i], argc);

        if (validInput == false)
        {
            cout << "X" << endl;
            return 0;
        }
    }

    return 0;
}

Can someone tell me why this code worked 10 minutes ago but keeps failing now?

I keep getting unhandled exception error. In the debug menu I am entering 32 and 12.5.

The code fails each time I try to check i > 0;

bool CheckArgInputs(char* inputs[], int numInputs)
{
    const char validChars[] = ".,+-eE0123456789";
    bool tester = false;
    
    for (int i = 0; *inputs[i] != 0; i++)
    {
        tester = false;

        for (int j = 0; j < sizeof(validChars); j++)
        {
            if (*inputs[i] == validChars[j])
            {
                tester = true;
            }
        }
        if (tester == false)
            return false;
        //else
        //  cout << "Good Input" << endl;
    }
}

int main(int argc, char* argv[])
{
    bool validInput = true;

    for (int i = 1; i < argc; i++)
    {

        validInput = CheckArgInputs(&argv[i], argc);

        if (validInput == false)
        {
            cout << "X" << endl;
            return 0;
        }
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2025-02-08 05:08:57

您的checkArginputs()函数被编码以表明其指向单个字符串的指针,但是main()实际上是给它的指针细绳。然后,该函数无法正确编码以迭代该字符串的单个字符,实际上它是通过argv []数组一次的一个字符串迭代的,直到它超出数组的范围。

如果输入字符串有效,则实际上也没有返回'从chode> chodarginputs()中使用任何内容。

而是尝试一下:

bool CheckArgInput(const char* input)
{
    const char validChars[] = ".,+-eE0123456789";
    
    for (int i = 0; input[i] != 0; i++)
    {
        for (int j = 0; j < sizeof(validChars)-1; j++)
        {
            if (input[i] != validChars[j])
            {
                return false;
            }
        }
    }

    return true;
}

int main(int argc, char* argv[])
{
    bool validInput = true;

    for (int i = 1; i < argc; i++)
    {
        validInput = CheckArgInput(argv[i]);
        if (!validInput)
        {
            cout << "X" << endl;
            return 0;
        }
    }

    return 0;
}

Your CheckArgInputs() function is coded to act like it is being given a pointer to an individual string, but main() is actually giving it a pointer to a pointer to a string. And then the function is not coded correctly to iterate the individual characters of just that string, it is actually iterating through the argv[] array one string at a time until it goes out of bounds of the array.

You are also not actually return'ing anything from CheckArgInputs() if the input string were valid.

Try this instead:

bool CheckArgInput(const char* input)
{
    const char validChars[] = ".,+-eE0123456789";
    
    for (int i = 0; input[i] != 0; i++)
    {
        for (int j = 0; j < sizeof(validChars)-1; j++)
        {
            if (input[i] != validChars[j])
            {
                return false;
            }
        }
    }

    return true;
}

int main(int argc, char* argv[])
{
    bool validInput = true;

    for (int i = 1; i < argc; i++)
    {
        validInput = CheckArgInput(argv[i]);
        if (!validInput)
        {
            cout << "X" << endl;
            return 0;
        }
    }

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文