I have the following code and when I compiled and went, I got following message
terminate called throwing an exceptionAbort trap: 6
I looked for this ,but I was not able to understand this error message. Please tell me, what this error message means and how I fix the code.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n,r;
while(cin>>n>>r){
if(n==0&&r==0)break;
string s;
for(int i=0;i<n;i++)
s[i]='i';
for(int i=0;i<r;i++){
int p,c;
cin>>p>>c;
string left=s.substr(p-1,p+c-1);
string right=s.substr(0,p-2)+s.substr(p+c, (int)s.size());
s=left+right;
}
cout << (int)(s[0]-'0')+1 << endl;
}
return 0;
}
compile with g++, on Mac OSX 10.7.4
Don’t know where the exception comes from, but this just doesn’t work
because the string is initially empty, and all
s[i]will be out of bounds.To add characters to the string in a loop, try
If you just want a string with lots of
'i's, you can do that when constructing it: