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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…