Can someone explain me, why my code is not working?

my code doesn't work meme
why is my code not working python
why doesn't my code work
debugging code practice
my code doesn't work facebook

I am completely new in C++ and I have to solve a task for college, where I have to make a struct Matrix and fill it with random integers. I marked the line with a "!" where the error appears. It is the error C2131(Visual C++ Compiler). It says "expression did not evaluate to a constant".

struct Matrix{
   int rows;
   int columns;
   Matrix(int r, int c){
      rows = r, columns = c;
   }
   int produceMatrix(){
       int matrix[rows][columns];  "!"
       for(int i = 0; i != rows; i++){
           for(int j = 0; j != columns; j++){
               matrix[i][j] = rand() %10 +1;
           }
       }
       return 0;
   }
   int showMatrix(){
       for(int i = 0; i != rows; i++){
           for(int j = 0; j != columns; j++){
               cout << matrix[i][j]<< endl;
           }
       }
   }
};


int main()
{
    srand(time(0));
    Matrix a(3, 4);

}    

If you are planning to create your matrix with rows and columns values only known at runtime, you are better off using std::vector<std::vector<int>> to hold your data, as the static array you use needs to know its size at compile time. But if all your sizes are known at compile time and you just want flexibility of creating different matrix sizes, you can use template, for example:

#include <iostream>
#include <ctime>

template <int ROWS, int COLUMNS>
struct Matrix
{
    int rows = ROWS;
    int columns = COLUMNS;
    int matrix[ROWS][COLUMNS] = {};

    void produceMatrix()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                matrix[i][j] = rand() % 10 + 1;
            }
        }
    }

    void showMatrix()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                std::cout << matrix[i][j] << "\t";
            }
            std::cout << std::endl;
        }
    }
};


int main()
{
    srand(time(0));
    Matrix<3,4> a;
    a.produceMatrix();
    a.showMatrix();
}

https://ideone.com/rCLxSn

4   10  5   5   
3   8   3   6   
2   4   9   10

Why isn't my code working?? i need help!!, I have tried everything I know but its still not working. will someone please tell me exactly what I need to type because I have been stuck since yesterday and I  What to do when your code doesn't work? I've seen a lot of people ask me this, so I decided to explain the most common problems I came across. My Code Does Not Work Because I Am A Victim Of

One thing is that you cannot make variable-length arrays this way. Another thing is that if you create a variable within a function (like you were doing here with int matrix in produceMatrix()), it is then not visible in another function.

Therefore, the array containing the elements of the matrix should be declared in your struct there, where you have declared rows and columns.

To store the elements of your matrix, you can use one-dimensional array of length equal to rows*columns.

Now, you need some kind of dynamic array to be able to make it of the length not known in the compilation time. One solution is to use a pointer and define an array with new operator in the constructor. However, if you use new, then you have to use delete at some point to deallocate memory, which here means that the destructor is needed. Another problem would be with copying of your matrices. Another, simpler solution is to use std::vector, a container provided by c++ standard library. Here's how to do it with std::vector (you need to add #include<vector> to your file):

struct Matrix{
int rows;
int columns;
vector<int> matrix;

Matrix(int r, int c){
rows = r, columns = c;
matrix = vector<int>(c*r);
}
int produceMatrix(){

    for(int i = 0; i < matrix.size(); i++){
        matrix[i] = rand() %10 +1;
    }
    return 0;
}
int showMatrix(){
    for(int i = 1; i <= matrix.size(); i++){
        cout << matrix[i-1];
        if(i%columns == 0) cout << endl;
        else cout << " ";
    }
    return 0;
}
};

Why is my code not working as intended?, May someone please explain to me what is wrong here? You are comparing string, not object. So == won't work. This can be accomplished by using String.​equals() method: if(wordAsArray[i].equals(userGuess)) I have not reviewed your code deeply, but there are some issues about your code (and  You're defining a numerical function and, writing 7 like this, you're defining its argument as a text string, which causes the if statement not to work. In that code you need an integer, not a string, and so, receiving an incorrect input, Python returns an incorrect output. The correct code is : elif days >= 3 and days < 7:

As many people commented, please go through a good C++ book to learn about arrays, classes, structs etc. As for your code, the following might produce what I think you want:

#include <iostream>
#include <vector>

struct Matrix
{
    int rows;
    int columns;
    std::vector<std::vector<int>> matrix;

    Matrix(int r, int c): rows(r), columns(c)
    {
        matrix.resize(r);
        for(int i = 0; i < r; i++)
            matrix[i].resize(c);
    }
    int produceMatrix()
    {
        for(int i = 0; i != rows; i++)
            for(int j = 0; j != columns; j++)
               matrix[i][j] = rand() %10 +1;
        return 0;
    }

    int showMatrix()
    {
        for(int i = 0; i != rows; i++)
        {
            for(int j = 0; j != columns; j++)
                std::cout << matrix[i][j]<<" ";
        }
        std::cout<<'\n';
    }
};

int main()
{
    srand(time(0));
    Matrix a(3, 4);
    a.produceMatrix();
    a.showMatrix();
}

10 Debugging Tips for Beginners: How to Troubleshoot and Fix Your , I realized that the process of troubleshooting and fixing the bugs in your code the tips I still follow day-to-day when debugging issues with my own code. time for over 7 years, I use these steps myself constantly (especially #9). If you can't seem to figure out what your error message is trying to tell you,  Re: can soemone explain this code to me 807569 May 17, 2006 1:20 PM ( in response to 807569 ) Best stall graffiti I ever read was in the Engineering building of my University. it was written in very small lettters near the bottom of the stall wall, so you really had to lean to read it.

8 barriers to overcome when learning to code, In the future you might need to fix hidden problems in this code, In this case, just read the theory again, or find someone to explain it to you if it still doesn't make sense. How can I prevent myself from making it in the future? I have not reviewed your code deeply, but there are some issues about your code (and may be there are some more): Most important ones is you are defining guessedinsideforloop, so every time the array is initialized to allfalse`.

The First Rule of Programming: It's Always Your Fault, But there's some bug or error you can't seem to get rid of. There I often find myself staring at the code, often to find no problems, I'll pass it to Am I just going to have to tell people who use my stuiff to NOT use eAccelerator? GOOD LUCK, Explainers! Remember, it's a five letter word you are looking for that unlocks the puzzle! First person to get it gets their comment pinned to the top as a tribute to their mighty brain

[JS] can someone explain to me how that code works?, now i atleast understand the script for opening and closing the navigation. so could someone explain to me, step by step, letter by letter how the dropdown code Having not spent enough time, I guarantee , you will find yourself going back. Those moments are my favourite thing about reading this site. A thorough, literal explanation of what the code is doing allows people who aren't familiar with the language to play along. I recently figured out a way to explain my code and meet those goals.

Comments
  • Because C++ does not have variable-length arrays. rows and columns need to be compile time constants to work
  • I am not that much familiar with C++ as well but I guess it is impossible to define functions inside of structs. You need to use "class" instead
  • @mangusta that's completely wrong. struct and class are the same thing in C++ (except for default access and inheritance modifier)
  • @UnholySheep yes, I re-checked the C++ specs, you are right
  • Additionally, a vector/array declared in one class method has absolutely nothing to do with anything used in another class method. You are attempting to declare a local array/vector in one class method, and use it in another class method. C++ does not work this way. Your array/vector must be a class member. Instead of going back and forth to stackoverflow.com with every question, you should simply read your C++ book. stackoverflow.com is not a C++ tutorial, and is not a substitute for a good C++ book.
  • What is the purpose of "resize" ?
  • @Josh.K: resize sets the size of the vector to the value we want, also allocating memory. So later, we can simply refer to the element with the operator[].