如何使用用户输入初始化2D数组?

时间:2018-03-16 22:20:39

标签: c++ visual-studio function matrix initialization

好的,所以我是c ++的新手,我真的需要帮助完成这项任务。因此,我必须基本上获取用户输入以填充两个矩阵,然后创建要添加的函数,输出到文件,输出到控制台以及用随机数填充矩阵。我已经完成了这个,但是当我尝试编译时,我不断收到这些错误:

警告C4390:';':发现空控制声明;这是意图吗?  警告C4700:未初始化的本地变量' Anum_rows'用过的 警告C4700:未初始化的本地变量' Anum_cols'用过的  警告C4700:未初始化的本地变量' Bnum_cols'用过的  警告C4700:未初始化的本地变量' Bnum_rows'使用

那我做错了什么? (这些函数在main之上,这里没有图示,但是使用变量Anum_rows,Anum_cols /也没有图示我声明了const max行和列。)

以下是供您参考的代码段:

 int main() {
srand((int)time(0));
int Anum_rows;
int Anum_cols;
int Bnum_rows;
int Bnum_cols;
int arr[10][10];
int b_arr[10][10];
int c_arr[10][10];
string random;
string answer;
string file;
string file_name;
ifstream fin;
while (Anum_rows == 1 && Anum_rows <= 10 && Anum_cols == 1 && Anum_cols <= 10) {



    cout << "Enter the number of rows for matrix A: ";
    cin >> Anum_rows;
    cout << "Enter the number of columns for matrix A: ";
    cin >> Anum_cols;
}

if (Anum_rows != 1 && Anum_rows != 10 && Anum_cols != 1 && Anum_cols != 10){
    cout << "Re-enter your dimensions: ";

1 个答案:

答案 0 :(得分:0)

你可能想要这样的东西:

 int main() {
srand((int)time(0));
int Anum_rows;
int Anum_cols;
int Bnum_rows;
int Bnum_cols;
int arr[10][10];
int b_arr[10][10];
int c_arr[10][10];
string random;
string answer;
string file;
string file_name;
ifstream fin;
int try_times = 0;
do{

    if(try_times != 0){
        cout << "Re-enter your dimensions: ";
    }

    try_times++;

    cout << "Enter the number of rows for matrix A: ";
    cin >> Anum_rows;
    cout << "Enter the number of columns for matrix A: ";
    cin >> Anum_cols;
}while (Anum_rows > 0 && Anum_rows <= 10 && Anum_cols > 0 && Anum_cols <= 10);

//if (Anum_rows != 1 && Anum_rows != 10 && Anum_cols != 1 && Anum_cols != 10){
//  cout << "Re-enter your dimensions: ";