terminate called throwing an exceptionAbort trap: 6

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

Answers


Don't know where the exception comes from, but this just doesn't work

string s;
for(int i=0;i<n;i++) 
    s[i]='i';

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

string s;
for(int i=0;i<n;i++) 
    s.push_back('i');

If you just want a string with lots of 'i's, you can do that when constructing it:

string s(n, 'i');

Need Your Help

Varying sized views in SWT

java swt eclipse-rcp

I'm working on laying out a perspective in SWT. To make things simple, it has no editor, just 1 or more views. There is a default view that should take the entire viewer area by default (can be

remove quote from array without using JSON_NUMERIC_CHECK

javascript php highcharts

I am trying to plot a scatter plot using highcharts.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.