在C++中使用用户输入变量声明数组大小。不同的 IDE 会产生不同的结果?
Declaring Array Size with a User Input Variable in C++. Different results in different IDE's?
我需要创建一个程序,用户输入数组的所需大小,然后C 代码创建它,然后允许数据输入。
这在代码中有效,但在Visual Studio社区2015
中不行当我将以下代码放在CodeBlocks版本13.12中时,它可以工作
#include<iostream>
using namespace std;
int main()
{
int count;
cout << "Making the Array" << endl;
cout << "How many elements in the array " << endl;
cin >> count;
int flex_array[count];
for (int i = 0; i < count; i = i + 1)
{
cout << "Enter the " << i << " term " << endl;
cin >> flex_array[i];
}
for (int j = 0; j < count; j = j + 1)
{
cout << "The " << j << " th term has the value " << flex_array[j] << endl;
}
return 0;
}
但是,如果我在Visual Studio 2015中输入相同的代码(即版本14.0.25425),我会收到错误:
表达必须具有恒定值
知道为什么会发生这种情况?
c 没有可变的长度数组。一些编译器实施是 extension ,但它仍然不是C 语言的标准功能,也不是便携式的。
如果您想要一个运行时变量长度阵列,则使用std::vector
:
std::vector<int> flex_array(count);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- C++:如何在CLion IDE中安装Boost
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 为什么这个C++程序在代码::块和在线 IDE 之间返回不同的结果?
- 相同的代码在联机 IDE 和本地 IDE 中给出不同的结果
- 在C++中使用用户输入变量声明数组大小。不同的 IDE 会产生不同的结果?
- 如何确保程序在 IDE 具有输出结果后停止运行
- 不同的c++IDE会产生不同的结果
- LPSolve c++源库和IDE给出了不同的LP任务求解结果