title: std::string Array Element Access
body: <p>Despite being a Project Euler program, the following code doesn't actually concern it much. I want to add 50 100-digit numbers, and I'm assigning each digit of each number to an element in the array addends[100][50]. I'd then add up each digit/place individually, and carry over extra digits. The numbers are being read in from a text file named <code>Input.txt</code>, and it merely contains all the numbers. <a href="http://projecteuler.net/problem=13" rel="nofollow">http://projecteuler.net/problem=13</a></p>
  
  <p>I'm having trouble assigning characters to elements of a string array (<code>string numbers[100][50]</code>) from a file input stream (<code>&lt;fstream&gt;</code>). The problem is described more completely in the comments:</p>
  
  <p>"[for the 1st loop] This loop assigns a number to every string in the string array. Even though the second number (50) doesn't do anything (it seems to be overridden by std::string; see variable declaration), it needs to be there for the loop to work. Same "logic" for the loop; "j" doesn't do anything but needs to be there for the loop to work?"</p>
  
  <p>And also, (for the 2nd loop) "This loop fills in the "addends[100][50]" array from the corresponding string array element. If I try to call "char_to_int()" with the array"numbers[i][j]", the compiler complains that the input isn't of the right data type. Adding a variable "k" makes the loop work for one run, but eventually crashes on the second loop (using "numbers[i][j][k]"). So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler complains that "const char *" is incompatible with "char". Adding a pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"), but the program still crashes later." I took out some code that doesn't matter to make it more readable.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  int char_to_int(char chInput);
  
  int main()
  {
  
      int placeholder;    //so console doesn't close immediately upon finish
      int sum[102] = {0}; // 100+2, 100 places + 2 places from carrying over
      int addends[100][50] = {0};
      string numbers[100][50];
      ifstream input("Input.txt");
  
      /* This loop assigns a number to every string in the string array. Even
       * though the second number (50) doesn't do anything (it seems to be
       * overridden by std::string; see variable declaration), it needs to be
       * there for the loop to work. Same "logic" for the loop; "j" doesn't do
       * anything but needs to be there??? Confused :-\
       */
      for (int i = 0; i &lt; 100; i++)
          for (int j = 0; j &lt; 1; j++)
              getline(input, numbers[i][j]);
  
      /* This loop fills in the "addends[100][50]" array from the corresponding
       * string array element. If I try to call "char_to_int()" with the array
       * "numbers[i][j]", the compliler complains that the input isn't of the
       * right data type. Adding a variable "k" makes the loop work for one run,
       * but eventually crashes on the second loop (using "numbers[i][j][k]").
       * So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler
       * complains that "const char *" is incompatible with "char". Adding a
       * pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"),
       * but the program still crashes on the second loop through.
       */
      for (int i = 0; i &lt; 100; i++)
          for (int j = 0; j &lt; 50; j++)
              for (int k = 0; k &lt; 1; k++) //used when the variable "k" was being used
                  addends[i][j] = char_to_int( (numbers[i][j]).c_str() );
  
      return 0;
  }
  </code></pre>
  
  <p>The code isn't finished; I decided against going on since I (obviously) need to fix this first.</p>