title: c++ Expression: string subscript out of range
body: <p>When I try debugging the code, it runs into the debugging error "c++ Expression: string subscript out of range"
  Pretty sure the problem was brought while calling setCode().
  How do I fix the code inside setCode()?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;list&gt;
  using namespace std;
  
  class test
  {
      private:
          string code;
          int digit;
  
      public:
          //constructor
          test(): code(""), digit(0) { }
  
          //copy constructor
          test(const test &amp;other):
          digit(other.digit)
          { 
              for(unsigned int i=0; i &lt; code.length(); i++)   
                  code[digit] = other.code[digit];
          }
  
          //set up the private values 
          void setCode(const string &amp;temp, const int num);
          void setDigit(const int &amp;num);
  
          //return the value of the pointer character 
          const string &amp;getCode() const;
          const unsigned int getDigit() const;
  };
  
  const string&amp; test::getCode() const
  {
      return code;
  }
  const unsigned int test::getDigit() const
  {
      return digit;
  }
  void test::setCode(const string &amp;temp, int num) 
  {
      code[num] = temp[num];  
  }
  void test::setDigit(const int &amp;num)
  {
      digit = num;
  }
  
  
  int main()
  {
      string contents = "dfskr-123";
  
      test aisbn;
      list&lt;test&gt; simul;
      list&lt;test&gt;::iterator testitr;
      testitr = simul.begin();
      int count = 0;
  
      cout &lt;&lt; contents &lt;&lt; '\n';
      aisbn.setCode(contents, count);
      aisbn.setDigit(count);
      simul.push_back(aisbn);
      count++;
  
      /*for(; testitr !=simul.end(); simul++)
      {
          cout &lt;&lt; testitr-&gt;getCode() &lt;&lt; "\n";
      }*/
  
  }
  </code></pre>