0

I'm trying to pass a 2D array into a function with the user's input (asking the user for the number of columns, rows, etc. Here's the code I have. Is there a way to pass the function with said inputs?

#include <iostream> // Input and output
#include <iomanip> // Input manipulator
#include <array>
using namespace std;

int rows;
int columns;

int getTotal(int array[][columns], int, int);


int main()
{
    cout << "How many rows would you like to have in your array? " << endl;
    cin  >> rows;
    
    cout << "How many rows would you like to have in your array? " << endl;
    cin >> columns;
    
    int userArray[rows][columns];
    
    
    getTotal(userArray, rows, columns); // This column will not call the function
    
    
    
    return 0;
}


int getTotal(int userArray[][columns], int rows, int columns)
{
    // function to add all numbers
}
  • So what exactly is the problem? – Mureinik Sep 10 '21 at 19:34
  • 2
    `int userArray[rows][columns];` is illegal in standard C++ without compiler extensions since the array sizes would need to be known at compile time, not run time. Your two options are using a std::vector (preferred) or using `new[]` (less preferred) see the above links. – Cory Kramer Sep 10 '21 at 19:36

0 Answers0