英文:
Expression must have a constant value error when creating an array C++
问题
抱歉,你的代码中存在一些问题,我会帮你指出并提供一些修复建议。以下是翻译好的部分:
Good day. Faced an error "Expression must have a constant value" when creating an array. Find this question <https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a constant-value#>, but these tips didn't solve the problem. Please help and I'm sorry for such requests, I just started to learn C ++
Code:
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <string>
#include <sstream>
#include <conio.h>
#include <random>
int main()
{
int userInput = 0, sumEvenIndexElements = 0, sumElementsBetweenZero = 0;
bool checkZeroElements = false;
std::cout << "Write array length:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
int *numbers = new int[lengthOfArray]; // 使用new分配动态数组
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(-10, 10);
for (int index = 0; index < lengthOfArray; index++)
{
numbers[index] = distribution(generator); // 存储随机数而不是分配新的int数组
std::cout << numbers[index] << std::endl;
if (index % 2 == 0)
{
sumEvenIndexElements += numbers[index]; // 累加偶数索引元素
}
}
std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;
for (int index = 0; index < lengthOfArray; index++)
{
if (numbers[index] == 0)
{
checkZeroElements = !checkZeroElements;
}
if (checkZeroElements)
{
sumElementsBetweenZero += numbers[index]; // 累加两个零之间的元素
}
}
if (!checkZeroElements)
{
std::cout << "Sorry, array has less than two zero elements.";
}
else
{
std::cout << "Sum elements between two zeros: " << sumElementsBetweenZero << std::endl;
}
delete[] numbers; // 释放动态分配的数组内存
return 0;
}
请注意,我已经做了一些修改来修复代码中的错误。希望这对你有所帮助。如果你有任何其他问题,请随时提问。
英文:
Good day. Faced an error "Expression must have a constant value" when creating an array. Find this question <https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value#>, but these tips didn’t solve the problem. Please help and I'm sorry for such requests, I just started to learn C ++
Code:
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <string>
#include <sstream>
#include <conio.h>
#include <random>
int main()
{
int userInput = 0, sumEvenIndexElements = 0, sumElementsBeetwenZero = 0;
bool checkZeroElements = false;
std::cout << "Write array length:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
int numbers [lengthOfArray];
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(-10, 10);
for (int index = 0; index < lengthOfArray; index++)
{
numbers[index] = new int[distribution(generator)];
std::cout << numbers << std::endl;
if (index % 2 == 0)
{
sumEvenIndexElements += *numbers[index];
}
}
std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;
for (int index = 0; index < lengthOfArray; index++)
{
numbers[index] = new int[distribution(generator)];
if (numbers[index] == 0)
{
checkZeroElements = !checkZeroElements;
}
if (checkZeroElements)
{
sumElementsBeetwenZero += *numbers[index];
}
}
if (checkZeroElements)
{
std::cout << "Sorry, array have less than two zero elements.";
}
else
{
std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;
}
}
答案1
得分: 1
lengthOfArray
是一个常量变量,它的值在初始化之后不会在运行时改变。
但它的值 - userInput
不是一个常量,它依赖于运行时用户的输入。正如在这里所指出的
常量值 是一个明确的数字或字符,例如1或0.5或'c'。
建议您使用std::vector而不是数组,如Paul Evans的答案中所建议的,或者为lengthOfArray
分配一个适当的常量值,该值将在编译时已知,例如:
const int lengthOfArray = 10;
英文:
lengthOfArray
is a constant variable, it's value would not be chagned during runtime after initialization.
But it's value - userInput
is not a constant, it dependes on runtime user input. As pointed here
> A constant value is an explicit number or character such as 1 or 0.5
> or ‘c’.
You should use std::vector instead of an array, as suggested in Paul Evans's answer, or assign a proper constant value to the lengthOfArray
, which will be known at the time of compilation, e.g.:
const int lengthOfArray = 10;
答案2
得分: 0
Your code:
std::cout << "写入数组长度:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
int numbers[lengthOfArray];
无法编译成功,根据你所遇到的编译器错误。
std::vector
更容易使用:
std::cout << "写入数组长度:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
std::vector<int> numbers(lengthOfArray, 0);
将创建一个长度为lengthOfArray
的std::vector
,并且所有元素都初始化为0
。
英文:
Your code:
std::cout << "Write array length:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
int numbers [lengthOfArray];
won't work as per the compiler error you're getting.
std::vector
plays a lot nicer:
std::cout << "Write array length:" << std::endl;
std::cin >> userInput;
const int lengthOfArray = userInput;
std::vector<int> numbers(lengthOfArray, 0);
will create a std::vector
of int
of length lengthOfArray
all initialised to 0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论