title: C++ Program abruptly ends after cin
body: <p>I am writing code to get the last digit of very large fibonacci numbers such as fib(239), etc.. I am using strings to store the numbers, grabbing the individual chars from end to beginning and then converting them to int and than storing the values back into another string. I have not been able to test what I have written because my program keeps abruptly closing after the <code>std::cin &gt;&gt; n;</code> line. 
  Here is what I have so far. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using std::cin;
  using std::cout;
  using namespace std; 
  
  char get_fibonacci_last_digit_naive(int n) {
  cout &lt;&lt; "in func";
  if (n &lt;= 1)
      return (char)n;
  
  string previous= "0";
  string current= "1";
  
  for (int i = 0; i &lt; n - 1; ++i) {
      //long long tmp_previous = previous;
      string tmp_previous= previous; 
  
      previous = current;
  
      //current = tmp_previous + current; // could also use previous instead of current
      // for with the current length of the longest of the two strings
      //iterates from the end of the string to the front
      for (int j=current.length(); j&gt;=0; --j) {
          // grab consectutive positions in the strings &amp; convert them to integers
          int t;
          if (tmp_previous.at(j) == '\0') 
              // tmp_previous is empty use 0 instead
              t=0;  
          else
              t = stoi((string&amp;)(tmp_previous.at(j))); 
          int c = stoi((string&amp;)(current.at(j)));
          // add the integers together
          int valueAtJ= t+c;
          // store the value into the equivalent position in current
          current.at(j) = (char)(valueAtJ); 
      }
      cout &lt;&lt; current &lt;&lt; ":current value"; 
  }
  
  return current[current.length()-1];
  }
  
  int main() {
  int n;
  std::cin &gt;&gt; n;
  
  //char&amp; c = get_fibonacci_last_digit_naive(n);  // reference to a local variable returned WARNING
  // http://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable
  cout &lt;&lt; "before call";
  char c = get_fibonacci_last_digit_naive(n);
  std::cout &lt;&lt; c &lt;&lt; '\n';
  
  return 0;
  }
  </code></pre>
  
  <p>The output is consistently the same. No matter what I enter for n, the output is always the same. This is the line I used to run the code and its output. </p>
  
  <pre><code>$ g++ -pipe -O2 -std=c++14 fibonacci_last_digit.cpp -lm
  
  $ ./a.exe
  10
  </code></pre>
  
  <p>There is a newline after the 10 and the 10 is what I input for n. 
  I appreciate any help. And happy holidays! </p>