Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

visual studio - Unhandled exception at in ConsoleApplication1.exe: Microsoft C++ exception: std::invalid_argument at memory location

I'm trying to make a basic calculator in Visual studio where the user enters an equation and the equation is solved by taking the equation as a string and then modifying the string to give the solved equation.But when I put the equation I get the error when debugging:

Unhandled exception at 0x00007FF9A1411F28 in ConsoleApplication1.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x000000195B4FF680.

Here's the code:

#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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry, Rookie mistake. Turns out i just assumed string.find would return a false value instead of string::npos.I edited the code so that it runs perfectly without any errors this time around

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

void Calculation(string &str);

int main()
{
string a;
cin >> a;
Calculation(a);
cout << a<<"
";
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);
    }
}

The Edits Include:

1.Added checks for string::nops in line 20,22,28.

2.Changed length of string to replace with as div_l instead of res_s.length() in line 40

Now,

taking an input =24/2/2

Output=6


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...