如何通过函数将输入输入数组?

时间:2019-03-09 14:18:39

标签: c++ arrays loops cin

我正在尝试创建一个函数,该函数接受一个数组并将用户输入馈入其中。

#include <iostream>

using namespace std;

void take_matrix_input(int rows,int columns, int matrix[rows][columns]);

int main() {
    int xyz[2][2];
    take_matrix_input(2,2,xyz);
}

void take_matrix_input(int rows,int columns, int matrix[rows][columns]){
    for (int i = 0; i < rows ; ++i) {
        for (int j = 0; j < columns; ++j) {
            cin >> matrix[i][j];
        }
    }
}

我收到以下错误:

/home/jagadeesh/Documents/clion-2018.1.2/bin/cmake/bin/cmake --build /home/jagadeesh/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 3
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
/home/jagadeesh/CLionProjects/untitled/main.cpp:5:61: error: use of parameter outside function body before ‘]’ token
 void take_matrix_input(int rows,int columns, int matrix[rows][columns]);
                                                             ^
/home/jagadeesh/CLionProjects/untitled/main.cpp:5:70: error: use of parameter outside function body before ‘]’ token
 void take_matrix_input(int rows,int columns, int matrix[rows][columns]);
                                                                      ^
/home/jagadeesh/CLionProjects/untitled/main.cpp:12:61: error: use of parameter outside function body before ‘]’ token
 void take_matrix_input(int rows,int columns, int matrix[rows][columns]){
                                                             ^
/home/jagadeesh/CLionProjects/untitled/main.cpp:12:70: error: use of parameter outside function body before ‘]’ token
 void take_matrix_input(int rows,int columns, int matrix[rows][columns]){
                                                                      ^
/home/jagadeesh/CLionProjects/untitled/main.cpp: In function ‘void take_matrix_input(...)’:
/home/jagadeesh/CLionProjects/untitled/main.cpp:13:25: error: ‘rows’ was not declared in this scope
     for (int i = 0; i < rows ; ++i) {
                         ^~~~
/home/jagadeesh/CLionProjects/untitled/main.cpp:14:29: error: ‘columns’ was not declared in this scope
         for (int j = 0; j < columns; ++j) {
                             ^~~~~~~
/home/jagadeesh/CLionProjects/untitled/main.cpp:15:20: error: ‘matrix’ was not declared in this scope
             cin >> matrix[i][j];
                    ^~~~~~
/home/jagadeesh/CLionProjects/untitled/main.cpp:15:20: note: suggested alternative: ‘main’
             cin >> matrix[i][j];
                    ^~~~~~
                    main
make[3]: *** [CMakeFiles/untitled.dir/build.make:63: CMakeFiles/untitled.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/untitled.dir/rule] Error 2
make: *** [Makefile:118: untitled] Error 2

错误消息非常隐蔽(此处为c ++ n00b),我不知道自己在做什么错。感谢您的帮助。

0 个答案:

没有答案