简体   繁体   English

C++ 一个无效参数被传递给一个认为无效参数致命的 function

[英]C++ An invalid parameter was passed to a function that considers invalid parameters fatal

So, I am trying to write a program that will eventually create a 2D array that will contain the location of mathematical operators in a user input string.因此,我正在尝试编写一个程序,该程序最终将创建一个二维数组,该数组将包含用户输入字符串中数学运算符的位置。 so, for example, if the user put in 2+5-3, I want my array to be something like {{+,1}{-,3}}.因此,例如,如果用户输入 2+5-3,我希望我的数组类似于 {{+,1}{-,3}}。 I intended to just use an integer array and a known translation from +,-,/,*,^ to 1,2,3,4,5 respectively.我打算只使用 integer 数组和从 +,-,/,*,^ 到 1,2,3,4,5 的已知翻译。 however I keep getting an exception thrown when I try to test it saying "string subscript out of range" and then my IDE puts up an error code on my if statement that reads "An invalid parameter was passed to a function that considers invalid parameters fatal".但是,当我尝试测试它说“字符串下标超出范围”时,我不断抛出异常,然后我的 IDE 在我的 if 语句上放置一个错误代码,内容为“无效参数已传递给认为无效参数致命的 function ”。 Any Ideas where I've messed up?我搞砸的任何想法?

#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <climits>

using namespace std;


int main()
{
equationstart:
    string eq;
    int posOp[50][2];
    int i;
    int i2 = 0;
    int i3;

    getline(cin, eq);
    for (i = 0; i <= 49; i++) {
        if (eq[i] == '+') {
            posOp[i2][0] = 1;
            posOp[i2][1] = i;
            i2++;
        }
    }

    for (i = 0; i <= 49; i++) {
        for (i3 = 0; i3 <= 1; i3++) {
            cout << posOp[i][i3];
        }
        cout << endl;
    }

    cout << endl;
    goto equationstart;

}

right now all I want it to do is fill the array then display the acquired array to the screen so I can see that it is working.现在我想做的就是填充数组,然后将获取的数组显示到屏幕上,这样我就可以看到它正在工作。

a) please don't use C arrays posOp[50][2] . a) 请不要使用 C arrays posOp[50][2] Use std::vector instead.改用 std::vector 。 It comes with range checking, if you use posOp.at(idx).如果您使用 posOp.at(idx),它会进行范围检查。 std::vector is one of the most basic and most important C++ features. std::vector 是 C++ 最基本和最重要的特性之一。

b) As @drescherjm pointed out in the comments above, eq[i] will trigger an exception (fortunately), when eq.size() is less than 50. Your loop runs from 0 to 49, which is fine for posOp , but eq may be shorter. b)正如@drescherjm 在上面的评论中指出的那样,当 eq.size() 小于 50 时, eq[i]将触发异常(幸运的是)。您的循环从 0 运行到 49,这对于posOp来说很好,但是eq 可能会更短。 You are accessing eq beyond it's end.您正在访问 eq 超出它的结尾。

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

相关问题 C++ 未处理异常:无效参数被传递给认为无效参数致命的函数 - C++ Unhandled exception: An invalid parameter was passed to a function that considers invalid parameters fatal 将无效参数传递给认为无效参数致命的 function? - An invalid parameter was passed to a function that considers invalid parameters fatal? 未处理的异常:一个无效的参数被传递给 function,它认为无效参数在 UWP 应用程序中是致命的 - Unhandled exception: An invalid parameter was passed to a function that considers invalid parameters fatal in UWP application C ++:无效的参数传递给C运行时函数 - C++: Invalid parameter passed to C runtime function 通过QTest将无效的参数传递给C运行时函数 - Invalid parameter passed to C runtime function with QTest 如何调试“传递给 C 运行时函数的参数无效”? - how to debug “Invalid parameter passed to C runtime function”? 必须在将QWidget和Invalid参数传递给C运行时函数之前构造QApplication - Must construct a QApplication before a QWidget & Invalid parameter passed to C runtime function RegisterClassEx失败为无效参数 - C ++ - RegisterClassEx Fails as Invalid Parameter - C++ 在函数C ++中由引用传递的指针参数 - pointer parameter passed by reference in a function c++ C ++内联函数,参数按值传递 - C++ inline function, parameter passed by value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM