【发布时间】:2018-04-17 21:59:59
【问题描述】:
所以我一直在尝试获取数组大小及其元素的输入,然后将元素显示到屏幕上,但是当我例如放 数组大小:7 数组元素:1 2 3 4 5 6 7 输出是:
1
2
3
4
5
6
6
代码:
#include <iostream>
using namespace std;
int main () {
int n , Arr[n];
cout << "please put the size of the array " ;
cin >> n;
cout << "please enter array's elemets ";
for (int k=0; k<n ; k++) {
cin >> Arr[k];
}
for (int i=0;i<n;i++){
cout << Arr[i] << endl;
}
}
【问题讨论】:
-
int n , Arr[n];是一个错误。实际上不止1个错误.. -
在标准 c++ 中,
n必须在编译时知道,因为数组的大小在编译时是固定的。同样对于允许此(VLA)通过扩展进行编译的非确认编译器,n仍需要在声明之前进行初始化。 -
即使使用 VLA 扩展,
n在使用时也未初始化 -
我建议读一本 C++ 的书,你学校的图书馆肯定有。
-
文件必须命名为“iostream .h” 不,这已经很久了。