【发布时间】:2018-04-25 17:50:36
【问题描述】:
不知道为什么当我打印检查时 for 循环不会在二维数组中保存正确的值。有什么想法吗?
#include <iostream>
using namespace std;
int row, col;
int main()
{
int num;
int val[row][col];
cout << "How many rows are there?" << endl;
cin >> row;
cout << "How many columns are there?" << endl;
cin >> col;
cout << "Enter values for the matrix: " << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> val[i][j];
}
}
return 0;
}
【问题讨论】:
-
int val[row][col];这不是有效的 C++ -
你甚至还没有初始化
row或col-> 未定义的行为(除了使用编译器扩展) -
@UnholySheep
row和col得到 zero-initialized,因为它们具有静态存储持续时间。
标签: c++ for-loop user-input nested-loops