0

Good day. Faced an error "Expression must have a constant value" when creating an array. Find this question 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;
   }

}
4
  • 1
    The answer is in the link you posted. "The standard requires the array length to be a value that is computable at compile time so that the compiler is able to allocate enough space on the stack." Commented Jan 3, 2020 at 10:38
  • Also see stackoverflow.com/questions/22013444/… Commented Jan 3, 2020 at 10:39
  • There are two types of arrays static and dynamic array. in the static array you must have a constant value because this will be generated at compile time. in the dynamic array you can put any number to initialize array. Commented Jan 3, 2020 at 13:55
  • one more thing, in the dynamic array you will have to delete it after using it. otherwise result will be in the leakage of memory. Commented Jan 3, 2020 at 13:57

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, have already seen this. Don't quite understand why it works that way.
@SergeyHolman , Did you get the difference between constant variable and constant value?
But is not a constant variable means that its value cannot be changed after initialization and the ability to use it as a constant value, it was logical or no?
No, because it is initialized with non-const value. Updated my answer with a ref to explanation, what is a const value.
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.

1 Comment

Thanks it's work. I think soon I will spend a lot of fun. Can you recommend a book on C++?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.