title: Dynamically create matrix from numerical input in C++
body: <p>I have a console application where I am trying to create a binary matrix from a numerical input.</p>
  
  <p>If a want to create a 2x4 matrix I must do two inputs inputs, one for each row. The input (console) will then look like this:</p>
  
  <p>First input: </p>
  
  <pre><code>1101
  </code></pre>
  
  <p>second input:</p>
  
  <pre><code>0111
  </code></pre>
  
  <p>And then I want to create a matrix that will look like this:</p>
  
  <pre><code>{
   1,1,0,1
   0,1,1,1
  }
  </code></pre>
  
  <p>When the user is typing <code>1101</code> in the console, the number should be splitted into its digits and each digit will be stored in different indexes in the columns. It is assumed that the user will only put in numbers of 1's and 0's. I will add a test for that later.</p>
  
  <p>All my attempts of this is creating the matrix in wrong formats and therefore when I try to read the data in the matrix I get wrong results.</p>
  
  <p>This is my attempt so far:</p>
  
  <p>Constructor that asks the user what size the matrix will be</p>
  
  <pre><code>Matrix::Matrix() {
      std::cout &lt;&lt; "Enter size of matrix:" &lt;&lt; std::endl;
      std::cout &lt;&lt; "Rows: ";
      std::cin &gt;&gt; rows;
      std::cout &lt;&lt; "Columns: ";
      std::cin &gt;&gt; cols;
  
      int EndX = rows;
      int EndY = cols;
      vect3 = CreateMatrix(rows); //This method is supposed to create the matrix
  }
  </code></pre>
  
  <p>The CreateMatrix method:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; Matrix::CreateMatrix(int row) {
      std::string number{};
      for (size_t i = 0; i &lt; row; i++) {
          std::cout &lt;&lt; "Enter row number " &lt;&lt; i &lt;&lt; ":" &lt;&lt; std::endl;
          std::cin &gt;&gt; number;
          for (size_t i = 0; i &lt; number.length(); i++)
              vect2.push_back(number[i] - '0');
          std::reverse(vect2.begin(), vect2.end());
          vect.emplace_back(std::move(vect2));
      }
      return vect;
  }
  </code></pre>
  
  <p>The CreateMatrix function is not creating the desired matrix that I want to create but i can't figure out what I am doing wrong.</p>
  
  <p>When I do this test later in the code</p>
  
  <pre><code>if (vect[row][col]) {
      // Some code
  }
  </code></pre>
  
  <p>The numbers are in the wrong place in the matrix so I this test i evaluating to true when it is expected to be false and vice versa.</p>
  
  <p>If I were to create the array manually on the stack with the example from abow it will look like this:</p>
  
  <pre><code>int matrix[2][4]{
               {1,1,0,1},
               {0,1,1,1}
           };
  </code></pre>
  
  <p>And if I do this test now:</p>
  
  <pre><code>if (matrix[row][col]) {
      // Some code
  }
  </code></pre>
  
  <p>The expression is evaluating to true and false as expected.</p>
  
  <p>so When I try to create the matrix dynamically by custom input the matrix does not have the correct format i.e. the numbers are in the wrong indexes. </p>
  
  <p>The problem is in the  <code>std::vector&lt;std::vector&lt;int&gt;&gt; Matrix::CreateMatrix(int row)</code> method but I can't fiure out what I am doing wrong so any help will be appreciated. </p>
  
  <p>Thanks!</p>
  
  <p>Matrix.h:</p>
  
  <pre><code>class Matrix {
  public:
      Matrix();
      std::vector&lt;std::vector&lt;int&gt;&gt; CreateMatrix(int);
      std::vector&lt;std::vector&lt;int&gt;&gt; getMatrix()const;; //Returns vect3
      ~Matrix();
  
  private:
      std::vector&lt;std::vector&lt;int&gt;&gt; vect{0};
      std::vector&lt;int&gt; vect2{0};
      std::vector&lt;std::vector&lt;int&gt;&gt; vect3{0};
  };
  </code></pre>
  
  <p>Edit:</p>
  
  <p>getMatrix:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; Matrix::getMatrix() const { return vect3; }
  </code></pre>
  
  <p>Testing:</p>
  
  <pre><code>Matrix matrixClass;
      std::vector&lt;std::vector&lt;int&gt;&gt; matrix = matrixClass.getMatrix(); //Returns vect3 from the Matrix class
  
      if (matrix[1][0]) //Should print false but prints true
          std::cout &lt;&lt; "true\n";
      else
          std::cout &lt;&lt; "false\n";
  
      int matrixxx[2][4]{
          {1,1,0,1},
          {0,1,1,1}
      };
  
      if (matrixxx[1][0]) //prints false
          std::cout &lt;&lt; "true\n";
      else
          std::cout &lt;&lt; "false\n";
  
      std::cin.get();
      std::cin.get();
      return 0;
  </code></pre>
  
  <p>Given the same matrix described abow:</p>
  
  <pre><code>    {
       1,1,0,1
       0,1,1,1
      }
  </code></pre>
  
  <p>The name <code>matrix</code> is the matrix that I try to create by user input. 
  When I test the number at index <code>[1][0]</code> the program should print <code>false</code> because the number at that index is a 0. But the program is printing <code>true</code> which means in some way there is a 1 at this index even though it should not be.</p>
  
  <p>The second attempt I create a matrix manually on the stack, I call it <code>matrixxx</code>and when I now try to acces this matrix number at index <code>[1][0]</code> the program do print <code>false</code> just as expected.</p>