title: std::length_error when trying to output a string from class array
body: <p>My program consists of three files. arithcard.hpp (header), assign2.cpp (the main program) and arithcard.cpp(method). </p>
  
  <p>In the array, cards, is store strings read from a text file. For example, cards[0].fileLine has the string 2 + 2 = 4. (fileLine is a private member variable with all the strings), cards[1].fileLine has the string 2 + 4 = 3, etc...</p>
  
  <p>I know cards[i].fileLine contains all these strings because I tested by printing them all out in the readCards method to see if they are actually in there. </p>
  
  <p>Here is my problem, when I try to call cards[i].getQuestion (or .getAnswer()) in the main program. Sometimes, it shows me the question(or answer) or the error</p>
  
  <p>"terminate called after throwing an instance of 'std::length_error'
    what():  basic_string::_S_create"</p>
  
  <h2>arithcard.hpp</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  /*
    Defines the class ArithCard, for handling arithmetic flash cards.
  
    You may not modify the public method declarations provided below. The main
    program used to test your solution will assume that these methods exist.
  
    You can add private attributes and methods as you need them.
  
    You may find it convenient to add additional public methods for testing.
    Keep in mind that your code must work with the unmodified main program
    or it will fail automated testing.
  */
  class ArithCard {
  
    public:
  
    // Default constructor
    ArithCard() ;
  
    static bool readCards(const std::string &amp;fileName,
              int &amp;cardCnt,ArithCard *&amp;cards) ;
  
    void displayQuestion(std::ostream &amp;out) ;
  
    bool checkAnswer(std::istream &amp;in) ;
  
    // Return the question as a string.
    std::string getQuestion() ;
  
    // Return the answer as a string.
    std::string getAnswer() ;
  
    // Return the answer as an integer.
    int getAnswerValue() ;
  
    private:
  
    // Add your private methods and attributes here.
    std::string fileLine;
    std::string question;
    std::string answer;
  } ;
  </code></pre>
  
  <h2>arithcard.cpp</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include "arithcard.hpp"
  using namespace std;
  
  ArithCard::ArithCard()
  {
      //body intentionally empty
  }
  
  bool ArithCard::readCards(const string &amp;fileName, int &amp;cardCnt, ArithCard *&amp;cards)
  {
      //read the file line by line and store it in cards
      return (true);
  }
  
  void displayQuestion(std::ostream &amp;out)
  {
     //display output
  }
  
   bool checkAnswer(std::istream &amp;in)
   {
       return (true);
   }
  
  // Return the question as a string.
   string ArithCard::getQuestion()
   {
       size_t pos = 0;
       pos = fileLine.find("=");
       question = fileLine.substr(0, pos);
       return question;
   }
  
   // Return the answer as a string.
    string ArithCard::getAnswer()
    {
        size_t pos = 0;
        pos = fileLine.find("=") + 1;
        answer = fileLine.substr(pos);
        return answer;
    }
  
      // Return the answer as an integer.
     int getAnswerValue()
     {
         return answer;
  
     }
  </code></pre>
  
  <h2>assign2.cpp</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;limits&gt;
  #include &lt;random&gt;
  #include "arithcard.hpp"
  
  // File-local anonymous namespace
  
  namespace {
  
  int verbosity = 1 ;
  
  }
  
  /*
    Read in the files of cards, then loop: query the user to specify a
    question, print the question, check the answer. Terminate when the user
    types an empty line instead of specifying a question.
  */
  int main (int argc, const char *argv[])
  {
    if (argc != 2) {
      std::cout &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;datafile&gt;" &lt;&lt; std::endl ;
      return (1) ;
    }
    std::string dataFile(argv[1]) ;
  
    bool result = false ;
    int cardCnt = -1 ;
    ArithCard *cards = nullptr ;
  
  /*
    Parse the file of questions &amp; answers. A false return value indicates an
    absent file or other i/o error. It's also possible the file is present but
    contains no valid questions.
  */
  result = ArithCard::readCards(dataFile,cardCnt,cards) ;
  std::cout &lt;&lt; cards[0].getQuestion() &lt;&lt; std:: endl;
  
    return (0) ;
  }
  </code></pre>