About 50 minutes ago I started working on a project where I convert a string with roman numerals to an integer. I am having an issue where there is an "array overflow", and I've been troubleshooting for 30 minutes, and am getting kind of frustrated because I can't find a solution. The error that is shown is as follows
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 3) >= this->size() (which is 3)
Here is my code:
class Solution {
public:
int romanToInt(string s) {
int result = 0;
char letters[7] = {'M','D','C','L','X','V','I'};
int numbers[7] = {1000,500,100,50,10,5,1};
for(int i=0; i<s.length(); i++) {
if(i!=(s.length()-1)) {
char *foo = std::find(std::begin(letters), std::end(letters), s.at(i));
char *nfoo = std::find(std::begin(letters), std::end(letters), s.at(i+1));
int num = numbers[std::distance(letters, foo)];
int num2 = numbers[std::distance(letters, nfoo)];
if(num<num2) {
result+=(num2-num);
}
else {
result+=num2;
}
}
else {
char *foo = std::find(std::begin(letters), std::end(letters), s.at(i));
int num = numbers[std::distance(letters, foo)];
result+=num;
}
}
return result;
}
};