Wow, kinda boggled by my first C++ program:
Considering the conditions in red, how is it possible that I get this at runtime:Code:#include <iostream>
#include <string>
using namespace std;
class nlist : public string {
public:
nlist(string in) {
assign(in);
}
int extract(size_t *pos) {
int retv = 0, m = 1;
size_t end, place;
while (at(*pos) < '0'|| at(*pos) > '9') {
// printf("%d %d\n",*pos,at(*pos)); fflush(stdout);
(*pos)++;
}
end = *pos;
while (at(end) > '0'&& at(end) < '9') end++;
place = end;
end--;
while (end>=*pos) {
if (end<0) break;
printf("pos %d end %d\n",*pos, end); fflush(stdout);
retv += (at(end--)-48)*m;
m*=10;
}
*pos = place;
return retv;
}
};
int main() {
string src="1,44,56,33\n";
nlist nums(src);
size_t len = nums.size(), pos = 0;
cout << nums;
while (pos<len) cout << nums.extract(&pos);
return 0;
}
1,44,56,33
pos 0 end 0
pos 0 end -1
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
Aborted
"If (end<0) break;" then it proceeds to the next line and reports end = -1.
???????????????