【问题标题】:Unhandled exception at in ConsoleApplication1.exe: Microsoft C++ exception: std::invalid_argument at memory locationConsoleApplication1.exe 中未处理的异常:Microsoft C++ 异常:内存位置的 std::invalid_argument
【发布时间】:2017-08-03 19:00:38
【问题描述】:

我正在尝试在 Visual Studio 中制作一个基本计算器,用户在其中输入一个方程,然后通过将方程作为字符串求解方程,然后修改字符串以给出求解方程。但是当我把方程调试时出现错误:

ConsoleApplication1.exe 中 0x00007FF9A1411F28 处未处理的异常: Microsoft C++ 异常:内存位置的 std::invalid_argument 0x000000195B4FF680.

代码如下:

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

void Calculation_div(string &str);

int main()
{
    string a;
    cin >> a;
    Calculation_div(a);
    cout << a;
}
void Calculation_div(string &str)
{
    std::size_t div_a;
    std::size_t div_r, div_l;
    while(str.find('/')) {
        div_a = str.find('/');
        if (str.find('/', div_a + 1)) {
        div_r = str.find('/', div_a + 1);
        }
        else {
            div_r = str.length();
        }
        if (str.rfind('/', div_a - 1) ) {
            div_l = str.rfind('/', div_a - 1) ;
        }
        else {
            div_l = 0;
        }
        string bi_l = str.substr(div_l, (div_a - div_l));
        string bi_r = str.substr(div_a+1, (div_r - div_a+1));
        int in_l = stoi(bi_l);
        int in_r = stoi(bi_r);
        int res_i = in_l + in_r;
        string res_s = std::to_string(res_i);
        str.replace(div_l, res_s.length(), res_s);
    }
}

【问题讨论】:

  • 同意,将调用堆栈向上移动到导致此错误的代码行。
  • 但是当我输入方程式时,我在调试时遇到错误 -- 如果您告诉我们重复问题的数据是什么,而不是隐藏cin 调用背后的数据。
  • 您能举例说明您输入的字符串a 的值是多少吗?你得到问题的方程示例?
  • 我使用的字符串的值为 a=22/22 虽然我确实发现当我声明 div_l=0 时问题就停止了,但它永远不会停止执行无限循环
  • 另外,请阅读std::string::find 的文档。找不到字符时返回npos,而不是您的代码假设的false

标签: c++ visual-studio c++11 visual-c++


【解决方案1】:

对不起,菜鸟的错误。结果我只是假设 string.find 会返回一个错误值而不是 string::npos。我编辑了代码,以便它这次完美运行而没有任何错误

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

void Calculation(string &str);

int main()
{
string a;
cin >> a;
Calculation(a);
cout << a<<"\n";
system("PAUSE");
}
void Calculation(string &str)
{
std::size_t div_a;
std::size_t div_r, div_l;
while(str.find('/') != string::npos) {
    div_a = str.find('/');
    if (str.find('/', div_a + 1) != string::npos) {
        div_r = str.find('/', div_a + 1);
    }
    else {
        div_r = str.length();
    }
    if (str.rfind('/', div_a - 1) != string::npos) {
        div_l = str.rfind('/', div_a - 1);
    }
    else {
        div_l = 0;
    }
    string bi_l = str.substr(div_l, (div_a - div_l));
    string bi_r = str.substr(div_a+1, (div_r - div_a+1));
    int in_l = stoi(bi_l);
    int in_r = stoi(bi_r);
    int res_i = in_l / in_r;
    string res_s = std::to_string(res_i);
    str.replace(div_l, div_r, res_s);
    }
}

编辑包括:

1.在第 20,22,28 行添加了对 string::nops 的检查。

2.在第 40 行更改了要替换为 div_l 而不是 res_s.length() 的字符串长度

现在,

接受输入 =24/2/2

输出=6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多