ConsoleApplication1.exe中未处理的异常:Microsoft C ++异常:内存位置处的std :: invalid_argument [英] Unhandled exception at in ConsoleApplication1.exe: Microsoft C++ exception: std::invalid_argument at memory location
问题描述
我正在尝试在Visual Studio中制作一个基本的计算器,用户可以在其中输入一个方程式,并通过将方程式作为字符串然后修改字符串以给出已求解的方程式来求解方程式。调试时出现错误:
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:
ConsoleApplication1.exe中0x00007FF9A1411F28的未处理异常:
Microsoft C ++异常:std :: invalid_argument在内存位置
0x000000195B4FF680。
Unhandled exception at 0x00007FF9A1411F28 in ConsoleApplication1.exe: Microsoft C++ exception: std::invalid_argument at memory location 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);
}
}
推荐答案
对不起,菜鸟错误。原来我只是假设string.find会返回一个假值而不是string :: npos。我编辑了代码,使其这次完美运行而没有任何错误
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<<"\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的检查。
1.Added checks for string::nops in line 20,22,28.
2。更改了字符串长度,用as替换div_l而不是第40行中的res_s.length()
2.Changed length of string to replace with as div_l instead of res_s.length() in line 40
现在,
接受输入= 24/2 / 2
taking an input =24/2/2
Output = 6
Output=6
这篇关于ConsoleApplication1.exe中未处理的异常:Microsoft C ++异常:内存位置处的std :: invalid_argument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!