Expression must have a constant value error when creating an array C++

huangapple go评论97阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;math.h&gt;
#include &lt;cstdlib&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;conio.h&gt;
#include &lt;random&gt;

int main()
{
   int userInput = 0, sumEvenIndexElements = 0, sumElementsBeetwenZero = 0;
   bool checkZeroElements = false;
   std::cout &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
   std::cin &gt;&gt; userInput;
   const int lengthOfArray = userInput;
   int numbers [lengthOfArray];
   std::default_random_engine generator;
   std::uniform_int_distribution&lt;int&gt; distribution(-10, 10);

   for (int index = 0; index &lt; lengthOfArray; index++)
   {
      numbers[index] = new int[distribution(generator)];
	  std::cout &lt;&lt; numbers &lt;&lt; std::endl;

	  if (index % 2 == 0)
	  {
		 sumEvenIndexElements += *numbers[index];
	  }

   }

   std::cout &lt;&lt; &quot;Sum even index elements: &quot; &lt;&lt; sumEvenIndexElements &lt;&lt; std::endl;

   for (int index = 0; index &lt; lengthOfArray; index++)
   {
	  numbers[index] = new int[distribution(generator)];

	  if (numbers[index] == 0)
	  {
		 checkZeroElements = !checkZeroElements;
	  }

	  if (checkZeroElements)
	  {
		 sumElementsBeetwenZero += *numbers[index];
	  }

   }

   if (checkZeroElements)
   {
	  std::cout &lt;&lt; &quot;Sorry, array have less than two zero elements.&quot;;
   }
   else
   {
	  std::cout &lt;&lt; &quot;Sum even index elements: &quot; &lt;&lt; sumEvenIndexElements &lt;&lt; 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);

将创建一个长度为lengthOfArraystd::vector,并且所有元素都初始化为0

英文:

Your code:

   std::cout &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
   std::cin &gt;&gt; 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 &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
   std::cin &gt;&gt; userInput;
   const int lengthOfArray = userInput;
   std::vector&lt;int&gt; numbers(lengthOfArray, 0);

will create a std::vector of int of length lengthOfArray all initialised to 0.

huangapple
  • 本文由 发表于 2020年1月3日 18:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定