C++ std::out_of_range error when I try to run the program
Okay so first off, Im pretty new to programming, Ive only read a bit of stuff and have been working on some project Euler problems to kind of wrap my head around concepts and such. However, I got an error message today that I couldn't make any sense of so I thought I would ask here for some help! Any links or advice is appreciated!
Here's the error message:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr Aborted
So any advice you might have would be awesome! If you need to see my code or have questions, ask! Though I''d rather try to understand the problem then find the answer myself! Thanks!
EDIT: Okay since you guys say you would need to see the code here it is.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int stringtoint(string s_convertee)
{
int i=0;
istringstream sin(s_convertee);
sin >> i;
return i;
}
int main()
{
string s_testnum = "233456091289474545356";
int n_maxmult开发者_开发技巧 = 0;
for (int i = 0; i<s_testnum.length(); i++)
{
int n_product = 1;
for (int j = i; j<(i+4); j++)
{
string s_multiplier = s_testnum.substr(j, 1);
int n_multiplier = stringtoint(s_multiplier);
n_product *= n_multiplier;
}
if (n_product>n_maxmult)
{
n_maxmult = n_product;
}
}
return 0;
}
From C++ Reference for substr,
If the position passed is past the end of the string, an out_of_range exception is thrown.
So my guess would be your calling substr
with a first parameter that's greater than the strings length.
Since you've posted your code, you can see,
i
can be a maximum of s_testnum.length()-1
,
but
j
goes up to i+4-1
= s_testnum.length()+2
.
You then call substr
with a first parameter of j
which as said can be longer than the string length. So there's the problem.
As other answers have already pointed out, in substr
If the position passed is past the end of the string, an out_of_range exception is thrown.
In your code:
for (int j = i; j<(i+4); j++)
When i
is 1
less than s_testnum.length()
j
goes past s_testnum.length()
and when you do, s_testnum.substr(j, 1);
causes an out_of_range exception.
Please post your code in question. Chances are you did something like:
std::string s("foo");
s.substr(5, 1); // The length of the string is 3, 5 is out of bounds
You're calling substr
with invalid parameters - you're trying to get element of the string, that is not there - for example trying to take the 10th char, when the string has only 5.
In your case, this is caused by substr
- you're trying to get a substring, that is too long for the pointed start position and it "goes out" of the real string.
terminate called after throwing an instance of 'std::out_of_range'
^^^^ says, that you have an uncaught exception, that is out_of_range
what(): basic_string::substr Aborted
^^^^ This is the text of the exception - note substr
Most likely that the parameter to the substr
function you called on some string in your code went past the string length. Hence the std::out_of_range
exception. But it is difficult to say without looking at the code. Also, you can step through the code and debug it yourself using a debugger like gdb/ddd. Just make sure to compile your code with the -g flag on g++.
You should be able to arrange for your debugger to break when an exception is thrown. Run the program under the debugger, set the appropriate breakpoint, and look at the stack walkback. (For the record, Ctrl+Alt+E should bring up a dialog box with the exception handling options in Visual Studios; the command catch throw
activates a breakpoint on an exception under gdb
.)
精彩评论