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;
   }

}
  • 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." – lars Jan 03 '20 at 10:38
  • Also see https://stackoverflow.com/questions/22013444/are-variable-length-arrays-there-in-c – lars Jan 03 '20 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. – Aqeel Ahmad Jan 03 '20 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. – Aqeel Ahmad Jan 03 '20 at 13:57

2 Answers2

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;
Nikita Smirnov
  • 813
  • 8
  • 17
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.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54